删除“onmousedown”通过Google Chrome扩展程序中的内容脚本从DOM元素发送事件 [英] Remove a "onmousedown" event from a DOM element via content script in Google Chrome extension

查看:418
本文介绍了删除“onmousedown”通过Google Chrome扩展程序中的内容脚本从DOM元素发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个谷歌浏览器扩展程序,用于从< a> 中移除 onmousedown 在网站上标记。

早些时候,该网站曾将 onmousedown 事件作为DOM的一部分,因此我可以通过内容脚本轻松地将其删除通过调用 element.removeAttribute(onmousedown)
但是现在,他们通过javascript添加了该事件,因此它不可能通过内容脚本直接移除该事件,因为它在不同的javascript上下文中运行。



PS:我特别在Google搜索结果页面上的链接上讨论 onmousedown 事件。此前,此一小段代码曾用于工作,但现在它不是 onmousedown 现在不是DOM的一部分。

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


解决方案

内容脚本无法访问页面的javascript对象(比如由javascript声明的onmousedown事件)。它们在沙箱中执行。



但是你可以做的是在页面的头部插入一个脚本(带脚本标签)以关闭事件:)


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

这篇关于删除“onmousedown”通过Google Chrome扩展程序中的内容脚本从DOM元素发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆