Baha imgur upload

add upload to imgur in bahamut

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

// ==UserScript==
// @name         Baha imgur upload
// @namespace    https://blog.maple3142.net/
// @version      0.4
// @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();
					if(!chk_isAuthorized()){
						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 upload(image.split('base64,')[1])
					}).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();
			}

			const $origUrlinput=$('#bhImgModeInsertUrl')
			if($origUrlinput.css('display')==='block'){
				if($('#bahaimgur_cvt').length)return;
				$('#bhImgImageUrl').after(`<button id="bahaimgur_cvt">轉換成imgur網址</button>`);

				$('#bahaimgur_cvt').on('click',e=>{
					e.preventDefault();

					if(!chk_isAuthorized()){
						login();
						return;
					}
					const url=$('#bhImgImageUrl').val();
					if(!url){
						alert('請輸入網址');
						return;
					}
					$("#bhImgMsg").html("圖片上傳中, 請稍候...").show();
					upload(url).then(r=>{
						if(!r.success){
							alert('上傳失敗');
							return;
						}
						$('#bhImgImageUrl').val(r.data.link);
						$("#bhImgMsg").html("圖片上傳中, 請稍候...").hide();
					})
				});
			}
			else{
				$('#bahaimgur_cvt').remove();
			}
		});
		observer.observe(document.body,{ attributes: true, childList: true, characterData: true,subtree: true });
	}
	function upload(image){
		return $.ajax({
			type: 'POST',
			url: 'https://api.imgur.com/3/image',
			data: {
				image: image
			},
			headers: {
				Authorization: `Bearer ${GM_getValue('access_token')}`
			},
			dataType: 'json'
		});
	}
	function chk_isAuthorized(){
		return GM_getValue('access_token',null)!==null;
	}
	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或关注我们的公众号极客氢云获取最新地址