Remove a “onmousedown” event from a DOM element vi

2019-08-08 17:57发布

问题:

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 :)