Remove a “onmousedown” event from a DOM element via content script in Google Chrome extension

那年仲夏 提交于 2019-12-24 12:24:39

问题


I'm trying to write a Google Chrome extension that removes the onmousedown event from a <a> tag on a website.

Earlier that site used to have onmousedown event as part of the DOM so I could easily remove it via content script by calling element.removeAttribute("onmousedown"). But now, they add that event via javascript so it's not possible for me directly remove that event via content script as it runs in a different javascript context.

PS: I'm specifically talking about onmousedown event on links on google search results page. Earlier, this small piece of code used to work but now it doesn't as onmousedown is not part of the DOM now.

var all_main_links = document.getElementsByClassName("r");
for (i=0; i<all_main_links.length; i++){
    var current_link = all_main_links[i].childNodes[0];
    current_link.removeAttribute("onmousedown");
    }

回答1:


A content script cannot access in page javascript object (such as onmousedown event declared by javascript). They're executed in a sandbox.

But what you can do is inserting a script in the head of the page (with script tag) wich shutdown the event :)



来源:https://stackoverflow.com/questions/24752464/remove-a-onmousedown-event-from-a-dom-element-via-content-script-in-google-chr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!