如何获取.getAttribute和.removeAttribute来匹配[onclick * ='ga']

问题描述 投票:0回答:1

我似乎无法获得Web扩展来阻止a [onclick * ='ga']作为属性。

我尝试使用

window.onload = function() {

let Removed = 0
const anchorElements = document.getElementsByTagName('A')

for (element of anchorElements) {
    if (!element.getAttribute('a[onclick*='ga']')) continue
            element.removeAttribute('a[onclick*='ga']')
            Removed += 1
            chrome.extension.sendMessage(Removed)
    }
}

window.onload = function() {

let Removed = 0
const anchorElements = document.getElementsByTagName('A')

for (element of anchorElements) {
    if (!element.getAttribute("onclick='ga'")) continue
            element.removeAttribute("onclick='ga'")
            Removed += 1
            chrome.extension.sendMessage(Removed)
    }
}

结果应该是扩展程序删除任何具有onclick属性为'ga'的链接,然后应在其中添加1,这将更新扩展程序徽章。

javascript google-chrome-extension firefox-addon firefox-webextensions
1个回答
0
投票

您的代码中有错误。这是静态内容的示例。如果内容是使用JavaScript生成的,则需要其他代码。

不需要window.onload

document.querySelectorAll('a[onclick*="ga"]').forEach(item => item.removeAttribute('onclick'));

如果您想记账

const a = document.querySelectorAll('a[onclick*="ga"]');
a.forEach(item => item.removeAttribute('onclick'));
chrome.runtime.sendMessage(a.length);

在上述循环内发送异步消息runtime.sendMessage不是一个好主意。

© www.soinside.com 2019 - 2024. All rights reserved.