简体   繁体   English

使用Chrome扩展程序取消绑定点击活动?

[英]Use Chrome extension to unbind click events?

I'm trying to make an extension that unbinds a click event added by the website itself. 我正在尝试制作一个扩展,解除网站本身添加的点击事件的绑定。

The website uses jQuery, which would make this insanely simple: 该网站使用jQuery,这将使这非常简单:

jQuery('a[rel]').unbind('click');

The problem is that my extension (using "content_scripts") can't access the website's jQuery object and therefore doesn't have the event functions to unbind. 问题是我的扩展(使用“content_scripts”)无法访问网站的jQuery对象,因此没有取消绑定的事件功能。 I can include a jQuery in my extension, but that wouldn't help, because jQuery stores 'data' in the jQuery object (not in the DOM elements). 我可以在我的扩展中包含一个jQuery,但这没有用,因为jQuery将'data'存储在jQuery对象中(而不是在DOM元素中)。 My jQuery will not have those events stored. 我的jQuery不会存储这些事件。

Is there another way? 还有另外一种方法吗? It doesn't have to be pretty. 它不一定非常漂亮。 Maybe without "content_scripts"? 也许没有“content_scripts”?

var unbind_event_listeners = function (node) {
    var parent = node.parentNode;
    if (parent) {
        parent.replaceChild(node.cloneNode(true), node);
    } else {
        var ex = new Error("Cannot remove event listeners from detached or document nodes");
        ex.code = DOMException[ex.name = "HIERARCHY_REQUEST_ERR"];
        throw ex;
    }
};

Just call unbind_event_listeners(a_node) to unbind any listeners from a node. 只需调用unbind_event_listeners(a_node)来取消绑定节点中的任何侦听器。 This will work on every node in the document except document itself. 这将适用于文档中除document本身之外的每个节点。 As for window , you're out of luck. 至于window ,你运气不好。 unbind_event_listeners(document.documentElement) should remove most event listeners attached to nodes in the document. unbind_event_listeners(document.documentElement)应该删除附加到文档中节点的大多数事件侦听器。

In the case of a[rel] , you'd want to do this: a[rel]的情况下,你想要这样做:

var nodes = document.querySelectorAll("a[rel]"), i = nodes.length;
while (i--) {
    unbind_event_listeners(nodes.item(i));
}

If it doesn't need to be pretty and you're okay with doing things slightly hack-like, this should forcefully unbind every click listener bound to that element: 如果它不需要漂亮而你可以做一些像hack一样的东西,这应该强制解除绑定到该元素的每个点击监听器:

var el = document.querySelector('a[rel]');
el.onclick = function() {};
el.addEventListener = function() {};

or for every element: 或者每个元素:

Array.prototype.slice.call(document.querySelectorAll('a[rel]')).forEach(function(el) {
  el.onclick = function() {};
  el.addEventListener = function() {};
});

EDIT: You could maybe do something even uglier and have a content script run at "document_start" and do: 编辑:你甚至可以做一些更丑陋的事情并在“document_start”运行一个内容脚本并执行:

Element.prototype.addEventListener = (function() {
  var real = Element.prototype.addEventListener;
  return function(ev) {
    if (ev === 'click' && this.tagName === 'A' && this.hasAttribute('rel')) {
      console.log('try again, jquery!');
    } else {
      return real.apply(this, arguments);
    }
  };
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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