// ==UserScript== // @name 能不能好好说话 // @namespace https://lab.magiconch.com/nbnhhsh // @version 0.1 // @description 首字母缩写划词翻译工具 // @author itorr // @match https://weibo.com/* // @match https://*.weibo.com/* // @match https://*.weibo.cn/* // @match https://tieba.baidu.com/* // @require https://cdn.bootcss.com/vue/2.6.11/vue.min.js // @grant none // @downloadURL none // ==/UserScript== let Nbnhhsh = ((htmlText,cssText)=>{ const API_URL = 'https://lab.magiconch.com/api/nbnhhsh/'; const request = (method,url,data,onOver)=>{ let x = new XMLHttpRequest(); x.open(method,url); x.setRequestHeader('content-type', 'application/json'); x.withCredentials = true; x.onload = ()=> onOver(x.responseText ? JSON.parse(x.responseText) : null); x.send(JSON.stringify(data)); return x; }; const Guess = {}; const guess = (text,onOver)=>{ text = text.trim(); if(Guess[text]){ return onOver(Guess[text]); } if(guess._request){ guess._request.abort(); } app.loading = true; guess._request = request('POST',API_URL+'guess',{text},data=>{ Guess[text] = data; onOver(data); app.loading = false; }); }; const submitTran = name=>{ let text = prompt('输入缩写对应文字',''); if(!text || !text.trim || !text.trim()){ return; } request('POST',API_URL+'translation/'+name,{text},()=>{ alert('感谢对好好说话项目的支持!审核通过后这条对应将会生效'); }); }; const getSelectionText = ()=>{ let text = getSelection().toString().trim(); if(!!text && /[a-z0-9]/i.test(text)){ return text; }else{ return null; } }; const fixPosition = ()=>{ let rect = getSelection().getRangeAt(0).getBoundingClientRect(); let scrollTop = document.documentElement.scrollTop || document.body.scrollTop; let top = Math.floor( scrollTop + rect.top +rect.height ); let left = Math.floor( rect.left ); app.top = top; app.left = left; }; const timer = ()=>{ if(getSelectionText()){ setTimeout(timer,300); }else{ app.show = false; } }; const nbnhhsh = ()=>{ let text = getSelectionText(); app.show = !!text; if(!app.show){ return; } fixPosition(); guess(text,data=>{ if(!data.length){ app.show = false; }else{ app.tags = data; } }); setTimeout(timer,300); }; const _nbnhhsh = ()=>{ setTimeout(nbnhhsh,1); }; document.body.addEventListener('mouseup',_nbnhhsh); document.body.addEventListener('keyup',_nbnhhsh); const createEl = (html)=>{ createEl._el.innerHTML = html; let el = createEl._el.children[0]; document.body.appendChild(el); return el; }; createEl._el = document.createElement('div'); createEl(``); const el = createEl(htmlText); const app = new Vue({ el, data: { tags:[], show:false, loading:false, top:0, left:0, }, methods:{ submitTran } }); return { guess, submitTran, } })(`