Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取.getAttribute和.removeAttribute以匹配[onclick*=';ga';]_Javascript_Google Chrome Extension_Firefox Addon_Firefox Addon Webextensions - Fatal编程技术网

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

Javascript 如何获取.getAttribute和.removeAttribute以匹配[onclick*=';ga';],javascript,google-chrome-extension,firefox-addon,firefox-addon-webextensions,Javascript,Google Chrome Extension,Firefox Addon,Firefox Addon Webextensions,我似乎无法让我的Web扩展阻止[onclick*='ga']作为属性 我试过使用 window.onload = function() { let Removed = 0 const anchorElements = document.getElementsByTagName('A') for (element of anchorElements) { if (!element.getAttribute('a[onclick*='ga']')) continue

我似乎无法让我的Web扩展阻止[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)
    }
}


结果应该是扩展删除了onclick属性为'ga'的任何链接,然后应该在删除的链接中添加1,这将更新扩展标记。

代码中有错误。 下面是一个静态内容的示例。如果内容是用JavaScript生成的,则需要额外的代码

不需要
窗口。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
不是一个好主意

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