Javascript 如何获取.getAttribute和.removeAttribute以匹配[onclick*=';ga';]
我似乎无法让我的Web扩展阻止[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
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);