Chrome 用户脚本

发布于 2024-09-07 07:33:42 字数 1297 浏览 3 评论 0原文

我在 Chrome 中遇到用户脚本问题。我已经在 Greasemonkey 中使用这个扩展的一个版本很长时间了,但我不知道如何在 Chrome 中解析 NodeList。这是 Greasemonkey 中的代码(请注意,该网站上的代码非常糟糕,因此占了我的大部分代码:

for each (table in response) {
        pre = table.getElementsByTagName('pre');
        for each (tag in pre) {
            if (findNotes.test(tag.textContent)) {
                time = tag.parentNode.parentNode.parentNode.childNodes[0].childNodes[3].childNodes[3].textContent;
                emailexp = new RegExp("*Excluded for anonymity*","g");
                email = emailexp.exec(tag.textContent);
                oldstr = "Note From: " + email;
                newstr = '<p style="font-weight:bold">Note From: ' + email + "\t" + time + "</p>"
                noteStr += tag.textContent.replace(oldstr, newstr);
                noteStr += "\n";
                var nodeToDelete = tag.parentNode.parentNode.parentNode.parentNode;
                nodeToDelete.removeChild(nodeToDelete.childNodes[1]);
            }
        }
    }

我遇到的问题是 Chrome 不支持“foreach”功能。因此,我能够将第一行修改为:

response.forEach(function(table, index, array) {

但每个的嵌套是我无法工作的部分。“pre”变量是一个 NodeList,我无法在其上使用 object.forEach 方法

。有人对我如何完成这项工作有任何建议吗?请注意,我正在做的任何事情都没有 ID 标签,这将使我的工作变得轻松(headdesk)。

I'm having a problem with a Userscript in chrome. I have been using a version of this extension in Greasemonkey for a long time now, but I can't figure out how to parse through a NodeList in Chrome. Here is the code as it is in Greasemonkey (please note that the code on the site is really bad, so that accounts for a lot of my code:

for each (table in response) {
        pre = table.getElementsByTagName('pre');
        for each (tag in pre) {
            if (findNotes.test(tag.textContent)) {
                time = tag.parentNode.parentNode.parentNode.childNodes[0].childNodes[3].childNodes[3].textContent;
                emailexp = new RegExp("*Excluded for anonymity*","g");
                email = emailexp.exec(tag.textContent);
                oldstr = "Note From: " + email;
                newstr = '<p style="font-weight:bold">Note From: ' + email + "\t" + time + "</p>"
                noteStr += tag.textContent.replace(oldstr, newstr);
                noteStr += "\n";
                var nodeToDelete = tag.parentNode.parentNode.parentNode.parentNode;
                nodeToDelete.removeChild(nodeToDelete.childNodes[1]);
            }
        }
    }

The problem I am having is that Chrome does not honor the 'for each' functionality. So, I was able to modify the first line into:

response.forEach(function(table, index, array) {

But the nested for each is the part I can't get to work. The 'pre' variable is a NodeList, which I can not use the object.forEach method on.

Does anybody have any suggestions on how I can make this work? Please note that there are no ID tags on anything I am working on, that would make my job to easy (headdesk).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2024-09-14 07:33:42

代替:

for each (tag in pre) {

使用:

for (var i=pre.length, tag; i >= 0; i--) {
  tag=pre.item(i);

Instead of:

for each (tag in pre) {

Use:

for (var i=pre.length, tag; i >= 0; i--) {
  tag=pre.item(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文