Baha imgur upload

add upload to imgur in bahamut

目前為 2017-12-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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());