用于替换加载到 Android 开源浏览器的页面正文标记中的文本的 JavaScript
我正在为适用于 Android 的开源浏览器编写 JavaScript,以替换加载到浏览器的页面body标记中的文本与一些不同的文字。
应该解决这个问题,一旦页面加载到浏览器中,该 JavaScript 就会执行 &更换发生&最后,带有替换文本的页面在浏览器中可见。
这是代码的替换部分:
var textnodes, node, i;
textnodes = document.evaluate("//body//text()[not(ancestor::script) and not(ancestor::style)]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
replace();
function replace() {
for (i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
text = node.data;
text = text.replace(/\'/g, "♥");
//The rest of the replacements
node.data = text;
}
}
但是 document.evaluate
似乎不起作用。任何人都可以帮助我更正此代码或以任何其他方式执行此替换正文任务的任何建议吗?
谢谢!
I'm writing a JavaScript for an open source browser available for Android to replace the text in the body tag of the pages loaded into the browser with some different text.
This should be worked in away that once a page get loaded into the browser, this JavaScript executes & the replacements take place & finally the page with replaced text is visible in the browser.
This is the replacing part of the code:
var textnodes, node, i;
textnodes = document.evaluate("//body//text()[not(ancestor::script) and not(ancestor::style)]",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
replace();
function replace() {
for (i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
text = node.data;
text = text.replace(/\'/g, "♥");
//The rest of the replacements
node.data = text;
}
}
However document.evaluate
seems to be not working. Can anyone help me to correct this code or any suggestions to do this replacing body text task in any other way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,您在 Android 浏览器中无法获取 DOM Level 3 XPath。
虽然您可以使用 JavaScript XPath 实现(eg),但这将是一个缓慢且缓慢的过程。与编写特定的 DOM 遍历代码相比,这是一个庞大的解决方案。
Sorry, you don't get DOM Level 3 XPath in the Android browser.
Whilst you can use a JavaScript XPath implementation (eg), that's going to be a slow and bulky solution compared to writing specific DOM traversal code.