// ==UserScript== // @name 鼠标取词获取翻译id // @namespace https://greasyfork.org/zh-CN/users/323663 // @version 1.0.4 // @description 应用于国际化非标项目,实现鼠标取词获取starling平台上对应的key。 // @match *://ads.tiktok.com/int* // @match *ads.tiktok.com/int* // @connect starling.snssdk.com // @grant GM_xmlhttpRequest // @author zhoubingling // @supportURL https://code.byted.org/zhoubingling.3365/findid-helper // @downloadURL none // ==/UserScript== (function() { var res = { '中': null, '日': null, '英': null }; var handleContainer = { currDom: null, messageList: [], createContainer: function() { const container = document.createElement("div"); container.setAttribute( "style", "" + "position:absolute!important;" + "font-size:13px!important;" + "overflow:auto!important;" + "background:#fff!important;" + "font-family:sans-serif,Arial!important;" + "font-weight:normal!important;" + "text-align:left!important;" + "color:#000!important;" + "padding:0.5em 1em!important;" + "line-height:1.5em!important;" + "border-radius:5px!important;" + "border:1px solid #ccc!important;" + "box-shadow:4px 4px 8px #888!important;" + "max-width:350px!important;" + "max-height:216px!important;" + "z-index:2147483647!important;" + "" ); this.currDom = container; return container; }, destroyContainer: function() { if (this.currDom && this.currDom.parentNode) this.currDom.parentNode.removeChild(this.currDom); this.currDom = null; } }; function findReact(dom) { let key = Object.keys(dom).find(key => key.startsWith("__reactInternalInstance$") ); let internalInstance = dom[key]; if (internalInstance == null) return null; if (internalInstance.return) { // react 16+ return internalInstance._debugOwner ? internalInstance._debugOwner.stateNode : internalInstance.return.stateNode; } else { // react <16 return internalInstance._currentElement._owner._instance; } } function ajax(url, method, headers) { if (!!!method) method = "GET"; // >>>因为Tampermonkey跨域访问(a.com)时会自动携带对应域名(a.com)的对应cookie // 不会携带当前域名的cookie // 所以,GM_xmlhttpRequest【不存在】cookie跨域访问安全性问题 // 以下设置默认headers不起作用<<< if (!!!headers) headers = { cookie: "" }; return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: method, url: url, headers: headers, onload: function(res) { return resolve(res.responseText); }, onerror: function(res) { reject("连接Starling失败"); } }); }); } window.onload = () => { if (!handleContainer.messageList.length) { const starlingUrlList = [ "https://starling.snssdk.com/text/7de818a0398f11e9bcb4d1ecac621e3e/index/zh", "https://starling.snssdk.com/text/7de818a0398f11e9bcb4d1ecac621e3e/index/ja", "https://starling.snssdk.com/text/7de818a0398f11e9bcb4d1ecac621e3e/index/en" ]; for (let i=0; i { let trs = JSON.parse(resText); let checkLang = starlingUrlList[i].split('/').pop(); if (checkLang === 'zh') { res['中'] = trs; } else if (checkLang === 'ja') { res['日'] = trs; } else { res['英'] = trs; } handleContainer.messageList.push(trs); }); }; } } document.onmouseout = e => { if (handleContainer.currDom && handleContainer.currDom == e.target) { e.preventDefault(); return; } handleContainer.destroyContainer(); }; document.onmouseover = e => { if (handleContainer.currDom && handleContainer.currDom == e.target) { e.preventDefault(); return; } handleContainer.destroyContainer(); let obj = findReact(e.target); let id = ""; let text = ""; if (obj && obj.props && obj.props.id) { id = obj.props.id; text = '' + id + '
' let container = handleContainer.createContainer(); container.style.top = e.pageY + "px"; if (e.pageX + 350 <= document.body.clientWidth) { container.style.left = e.pageX + "px"; } else container.style.left = document.body.clientWidth - 350 + "px"; for (let key in res) { text += '

' + key +': ' + res[key].message[id] + '

'; } container.innerHTML = text; document.body.appendChild(container); } }; })();