Baha imgur upload

add upload to imgur in bahamut

目前为 2017-12-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         Baha imgur upload
// @namespace    https://blog.maple3142.net/
// @version      0.2
// @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;
						}
						bahaRte.toolbar.insertUploadedImage(r.data.link);
					}).catch(e=>alert('上傳失敗'));
				});
			}
			else{
				console.log('remove');
				$('#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());

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址