添加了左侧悬浮图标、设置选项、动态更新

This commit is contained in:
woohoo 2021-12-27 21:09:27 +08:00
parent 13bfce86ed
commit b1f09a502a
2 changed files with 195 additions and 3 deletions

41
gengxin/index.html Normal file
View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>更新页面</title>
<script>
window.onload = () => {
const origin = {};
let intervalID;
const container = document.querySelector('.container p');
const btn = document.querySelector('.container button');
window.addEventListener("message", (e) => {
origin.origin = e.origin;
origin.source = e.source;
if (e.data === 'connect') e.source.postMessage('connected', e.origin)
}, false);
intervalID = window.setInterval(() => {
if (origin.origin) {
clearInterval(intervalID);
intervalID = null;
container.innerHTML = '已连接';
btn.disabled = false;
}
}, 1000);
btn.onclick = (e) => {
e.target.blur();
origin.source.postMessage('测试', origin.origin);
origin.source.focus();
window.close();
};
};
</script>
</head>
<body>
<h1>更新页面</h1>
<div class="container">
<p> </p>
<button disabled>测试</button>
</div>
</body>
</html>

View File

@ -1,8 +1,8 @@
// ==UserScript== // ==UserScript==
// @lastmodified 202112251422 // @lastmodified 202112272108
// @name Torn翻译 // @name Torn翻译
// @namespace WOOH // @namespace WOOH
// @version 0.1.1225a // @version 0.1.1227a
// @description Torn UI翻译 // @description Torn UI翻译
// @author Woohoo-[2687093] sabrina_devil[2696209] // @author Woohoo-[2687093] sabrina_devil[2696209]
// @match https://www.torn.com/* // @match https://www.torn.com/*
@ -23,6 +23,11 @@
todo: true, todo: true,
cont: `baza npc商店 imarket及imarket搜索结果`, cont: `baza npc商店 imarket及imarket搜索结果`,
}, },
{
ver: '0.1.1227a',
date: '20211227',
cont: `添加了左侧悬浮图标、设置选项、动态更新`,
},
{ {
ver: '0.1.1225a', ver: '0.1.1225a',
date: '20211225', date: '20211225',
@ -2819,6 +2824,89 @@
"December": "十二月", "December": "十二月",
}; };
// const transDict = {};
// transDict.titleDict = titleDict;
// transDict.titleLinksDict = titleLinksDict;
// transDict.sidebarDict = sidebarDict;
// transDict.tooltipDict = tooltipDict;
// transDict.statusDict = statusDict;
// transDict.miniProfileDict = miniProfileDict;
// transDict.homeDict = homeDict;
// transDict.attackDict = attackDict;
// transDict.newspaperDict = newspaperDict;
// transDict.propertyDict = propertyDict;
// transDict.travelingDict = travelingDict;
// transDict.tipsDict = tipsDict;
// transDict.cityDict = cityDict;
// transDict.gymDict = gymDict;
// transDict.gymList = gymList;
// transDict.eduDict = eduDict;
// transDict.headerDict = headerDict;
// transDict.eventsDict = eventsDict;
// transDict.chatDict = chatDict;
// transDict.hosDict = hosDict;
// transDict.awDict = awDict;
// transDict.playerTitleList = playerTitleList;
// transDict.ocList = ocList;
// transDict.profileDict = profileDict;
// transDict.sendCashDict = sendCashDict;
// transDict.stockDict = stockDict;
// transDict.itemPageDict = itemPageDict;
// transDict.itemNameDict = itemNameDict;
// transDict.itemDescDict = itemDescDict;
// transDict.itemEffectDict = itemEffectDict;
// transDict.itemTypeDict = itemTypeDict;
// transDict.itemReqDict = itemReqDict;
// transDict.tornSettingsDict = tornSettingsDict;
// transDict.missionDict = missionDict;
// transDict.pcDict = pcDict;
// transDict.npcShopDict = npcShopDict;
// transDict.calDict = calDict;
// if (!localStorage.getItem('wh_trans_transDict')) localStorage.setItem('wh_trans_transDict', JSON.stringify(transDict))
// 左侧标签
const zhongIconIntervalID = window.setInterval(() => {
if (!document.querySelector('#wh-trans-icon')) initIcon();
}, 3000);
const wh_trans_settings = localStorage.getItem('wh_trans_settings')
? JSON.parse(localStorage.getItem('wh_trans_settings'))
: {
transEnable: true,
attBtnPos: 2,
isDev: false,
};
initIcon();
addStyle(
`#wh-trans-icon{
display: inline-block;
position: fixed;
top: 50%;
left: 0;
z-index: 10;
background-color: white;
border: solid 1px #b5b5b5;
border-left: none;
border-radius: 0 4px 4px 0;
max-width: 200px;
}
#wh-trans-icon button{
margin:0;
padding:0;
border:0;
}
#wh-trans-icon .wh-container{
margin:0;
padding:0 16px 16px;
border:0;
}
#wh-trans-icon-btn{
height:16px;
width:16px;
background: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M160 144a32 32 0 0 0-32 32V864a32 32 0 0 0 32 32h688a32 32 0 0 0 32-32V176a32 32 0 0 0-32-32H160z m0-64h688a96 96 0 0 1 96 96V864a96 96 0 0 1-96 96H160a96 96 0 0 1-96-96V176a96 96 0 0 1 96-96z"/><path d="M482.176 262.272h59.616v94.4h196v239.072h-196v184.416h-59.616v-184.416H286.72v-239.04h195.456V262.24z m-137.504 277.152h137.504v-126.4H344.64v126.4z m197.12 0h138.048v-126.4H541.76v126.4z"/></svg>') no-repeat center;
padding:16px !important;
}
.wh-hide{display:none;}`);
// 默认开启通知翻译 // 默认开启通知翻译
if (!localStorage.getItem('wh_trans_event')) { if (!localStorage.getItem('wh_trans_event')) {
localStorage.setItem('wh_trans_event', 'true'); localStorage.setItem('wh_trans_event', 'true');
@ -5615,6 +5703,7 @@ margin: 0 0 3px;
} }
}); });
const ob = new MutationObserver(() => { const ob = new MutationObserver(() => {
ob.disconnect();
const $ct_title = $ct_wrap.querySelector('.status-title'); const $ct_title = $ct_wrap.querySelector('.status-title');
const $pos = $ct_wrap.querySelector('.map-title span[class^="position___"]') || $ct_wrap.querySelector('.status-title span[class^="position___"]'); const $pos = $ct_wrap.querySelector('.map-title span[class^="position___"]') || $ct_wrap.querySelector('.status-title span[class^="position___"]');
if (!$pos) return; if (!$pos) return;
@ -5683,7 +5772,7 @@ margin: 0 0 3px;
dropHist[hist_key] = { dropHist[hist_key] = {
pos: `[${nearby_item.x},${nearby_item.y}]`, pos: `[${nearby_item.x},${nearby_item.y}]`,
map: $ct_title.firstChild.nodeValue.trim(), map: $ct_title.firstChild.nodeValue.trim(),
last: `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`, last: `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`,
name: item_name, name: item_name,
id: Object.keys(dropHist).length, id: Object.keys(dropHist).length,
}; };
@ -5709,6 +5798,7 @@ margin: 0 0 3px;
}); });
$tbody.innerHTML = table_html; $tbody.innerHTML = table_html;
localStorage.setItem('wh-loot-store', JSON.stringify(dropHist)); localStorage.setItem('wh-loot-store', JSON.stringify(dropHist));
ob.observe($ct_wrap, {childList: true, subtree: true});
}); });
getDOMOb.observe($root, {childList: true, subtree: true}); getDOMOb.observe($root, {childList: true, subtree: true});
return; return;
@ -6727,4 +6817,65 @@ ${htmlCont}</div></div></div></div><hr class="delimiter-999 m-top10 m-bottom10">
mo.observe(dom, opt) mo.observe(dom, opt)
} }
} }
/*
添加全局style
*/
function addStyle(v) {
const wh_gStyle = document.querySelector('style#wh-trans-gStyle');
if (wh_gStyle) {
wh_gStyle.innerHTML += v;
} else {
const wh_gStyle = document.createElement("style");
wh_gStyle.id = 'wh-trans-gStyle';
wh_gStyle.innerHTML = v;
document.head.append(wh_gStyle);
}
}
/*
添加左侧图标
*/
function initIcon() {
const zhongNode = document.createElement('div');
zhongNode.id = 'wh-trans-icon';
zhongNode.innerHTML =
`<div><button id="wh-trans-icon-btn"></button></div>
<div class="wh-container wh-hide">
<div class="wh-main">
<div><b>翻译助手</b></div>
<div><button id="wh-trans-data-update">更新翻译词库数据</button></div>
<div><p>选项</p></div>
</div>
</div>`;
$('body').prepend(zhongNode);
zhongNode.querySelector('#wh-trans-icon-btn').onclick = (e) => {
e.target.blur();
$(e.path[1].nextElementSibling).toggleClass('wh-hide');
};
let intervalID;
let connected = false;
zhongNode.querySelector('#wh-trans-data-update').onclick = (e) => {
e.target.blur();
if (intervalID) return;
const url = wh_trans_settings.isDev ? 'http://localhost:8080/' : 'https://jjins.github.io/gengxin/';
const popup = window.open(url);
intervalID = window.setInterval(() => {
popup.postMessage("connect", url);
}, 1000);
};
window.addEventListener("message", (e) => {
if (e.data === 'connected') {
connected = true;
if (intervalID) clearInterval(intervalID);
intervalID = null;
return;
}
if (connected) {
// 传回的动态内容
// console.log(e.data)
if (e.data === '测试') alert(e.data);
}
}, false);
}
}()); }());