简体   繁体   中英

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

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");
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM