Baha imgur upload

add upload to imgur in bahamut

当前为 2017-12-26 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Baha imgur upload
// @namespace    https://blog.maple3142.net/
// @version      0.3.1
// @description  add upload to imgur in bahamut
// @author       maple3142
// @match        https://*.gamer.com.tw/*
// @match        https://blog.maple3142.net/bahamut-imgur-upload.html
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function($) {
	'use strict';

	if(location.hostname==='blog.maple3142.net'){
		const access_token=/access_token=(.*?)&/.exec(location.hash)[1];
		if(access_token){
			GM_setValue('access_token',access_token);
		}
	}
	else{
		console.log('Baha-imgur-upload');
		const observer=new MutationObserver(_=>{

			const $origUpl=$('#bhImgModeUpload');
			if($origUpl.css('display')==='block'){
				if($('#imgurupl').length)return; //exists, ignore it
				$origUpl.after(`
<div id="bahaimgur">
<input type="file" accept="image/*" id="imgurupl">
<button id="imguruplbtn">上傳imgur</button>
</div>
`);
				$('#imguruplbtn').on('click',e=>{
					e.preventDefault();
					let access_token;
					if(!(access_token=GM_getValue('access_token',false))){ //no access_token
						login();
						return;
					}
					const file=$('#imgurupl')[0].files[0];
					if(!file)return; //no file

					readbase64(file).then(image=>{
						$('#bahaimgur').hide(),$("#bhImgMsg").html("圖片上傳中, 請稍候...").show(), $("#bhImgModeUpload").hide();
						return $.ajax({
							type: 'POST',
							url: 'https://api.imgur.com/3/image',
							data: {
								image: image.split('base64,')[1],
								type: 'base64'
							},
							headers: {
								Authorization: `Bearer ${access_token}`
							},
							dataType: 'json'
						});
					}).then(r=>{
						if(!r.success){
							alert('上傳失敗');
							return;
						}
						if(unsafeWindow.bahaRte!=null){
							bahaRte.toolbar.insertUploadedImage(r.data.link);
						}
						else{
							prompt('暫時還不支援這種編輯器,不過可以複製下方的網址來貼上',r.data.link);
						}
					}).catch(e=>{
						console.error(e);
						alert('上傳失敗');
					});
				});
			}
			else{
				$('#bahaimgur').remove();
			}
		});
		observer.observe(document.body,{ attributes: true, childList: true, characterData: true,subtree: true });
	}
	function login(){
		window.open('https://api.imgur.com/oauth2/authorize?client_id=41e93183c27ec0e&response_type=token','oauth','height=700,width=700');
	}
	function readbase64(file){
		return new Promise((res,rej)=>{
			const reader=new FileReader();
			reader.onload=e=>res(e.target.result);
			reader.onerror=err=>rej(err);
			reader.readAsDataURL(file);
		})
	}
})(jQuery.noConflict());