-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【探讨】一种通用翻译替换脚本 #62
Comments
输入框提示等等功能就不好实现了 |
这是数据库的json格式吗? |
是
发自我的iPhone
… 在 2020年1月3日,下午6:03,枫谷剑仙 ***@***.***> 写道:
这是数据库的json格式吗?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
只要你的node能匹配attribute node,其实翻译内容和属性没啥区别 |
更新了结构 |
不知道能不能用eval |
脚本内可以用eval的 |
另外 可以使用tabs.executeScript 替代现在用的content_scripts 将数据直接构建到代码里,就可以摆脱对localStorage的依赖还能简化数据同步的逻辑,executeScript是异步的,不知道的可靠性怎么样,如果代码没有在body的dom加载之前注入的话可能会导致翻译失效。 browser.tabs.onUpdated.addListener(async (tabId: number, changeInfo, tab) => {
if(changeInfo['status'] != 'loading') return;
const id = tabId;
console.log('tab onUpdated', tabId, changeInfo, tab);
console.log(tagDatabase.tagList.value);
const code = `
console.log('test');
window.tagData = ${JSON.stringify(tagDatabase.tagList.value, null, 2)};
function test(node) {
if(node.nodeName == '#text' && node.parentNode && node.parentNode.nodeName != 'SCRIPT' )node.textContent = node.textContent + '🐹';
}
const observer = new MutationObserver(mutations => mutations.forEach(mutation =>
mutation.addedNodes.forEach(node1 => {
test(node1);
if (this.documentEnd && node1.childNodes) {
const nodeIterator = document.createNodeIterator(node1);
let node = nodeIterator.nextNode();
while (node) {
test(node);
node = nodeIterator.nextNode();
}
}
})
));
observer.observe(window.document, {
attributes: true,
childList: true,
subtree: true
});
`;
await browser.tabs.executeScript(id, {
code: `
const script = document.createElement('script');
script.innerHTML = \`${code.replace(/`/igm, '\\`').replace(/\\/igm, "\\\\")}\`;
document.head.appendChild(script);
`,
runAt: "document_start",
});
}); |
是不是可以通过拦截 HTTP 请求来实现翻译。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: