XUL 将事件附加到使用 javascript 添加的锚点

发布于 2024-08-08 03:36:01 字数 1536 浏览 9 评论 0原文

我正在编写一个 Firefox 扩展,它将事件侦听器添加到页面上的所有锚标记。基本上我有:

window.addEventListener("load", function() { myExtension.init(); }, false);

var myExtension = {

    init: function() {
    var appcontent = document.getElementById("appcontent");  
    if(appcontent)
        appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);  
    },

    onPageLoad: function(event) {
        var doc = event.originalTarget;
        var anchors = doc.getElementsByTagName("a");
        //attach event listeners
    }
}

这适用于页面上的大多数标签。问题是用 javascript 添加的标签不会附加事件侦听器。我尝试过重新定义 createElement:

//in the onPageLoadFunction
var originalCreateElement = doc.createElement;
doc.createElement = function(tag) {
    if (tag != "a"){
        return originalCreateElement(tag);
    }
    var anchor = originalCreateElement("a");
    anchor.addEventListener("mouseover", myInterestingFunction, false);
    return anchor;
}

并尝试添加 DOM 插入侦听器

//in the onPageLoadFunction
function nodeInserted(event){;
    addToChildren(event.originalTarget);
}

function addToChildren(node){
    if (node.hasChildNodes()){
        var nodes = node.childNodes;
        for (var i = 0; i < nodes.length; i++){
            addToChildren(nodes[i]);
        }
    }
    if (node.nodeName == "a"){
        anchorEvent(node); //adds event listeners to node
    }
}

doc.addEventListener("DOMNodeInserted", nodeInserted, false);

,但都不起作用。如何获取对这些锚点对象的引用,以便向它们添加侦听器?

谢谢

I'm writing a firefox extension that adds event listeners to all the anchor tags on a page. Essentially I have:

window.addEventListener("load", function() { myExtension.init(); }, false);

var myExtension = {

    init: function() {
    var appcontent = document.getElementById("appcontent");  
    if(appcontent)
        appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);  
    },

    onPageLoad: function(event) {
        var doc = event.originalTarget;
        var anchors = doc.getElementsByTagName("a");
        //attach event listeners
    }
}

This works for most of the tags on the page. The problem is that tags added with javascript don't get the event listener attached. I've tried redefining createElement:

//in the onPageLoadFunction
var originalCreateElement = doc.createElement;
doc.createElement = function(tag) {
    if (tag != "a"){
        return originalCreateElement(tag);
    }
    var anchor = originalCreateElement("a");
    anchor.addEventListener("mouseover", myInterestingFunction, false);
    return anchor;
}

and I've tried adding a DOM insertion listener

//in the onPageLoadFunction
function nodeInserted(event){;
    addToChildren(event.originalTarget);
}

function addToChildren(node){
    if (node.hasChildNodes()){
        var nodes = node.childNodes;
        for (var i = 0; i < nodes.length; i++){
            addToChildren(nodes[i]);
        }
    }
    if (node.nodeName == "a"){
        anchorEvent(node); //adds event listeners to node
    }
}

doc.addEventListener("DOMNodeInserted", nodeInserted, false);

but neither work. How can I get references to these anchor objects so I can add listeners to them?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-08-15 03:36:01

动态添加“事件侦听器到页面上的所有锚标记”很难有效地完成。您有两种选择:

  1. 定期轮询页面中的锚标记(如 jQuery.live() 所做的那样)
  2. 使用 DOM 突变事件(DOMNodeInserted 等)

前者对性能影响明显。使用突变事件会带来隐藏的性能成本:如果为文档添加任何突变事件,Gecko 在其 DOM 代码中会采用较慢的路径,因此所有 DOM 操作都会变慢。

为什么不使用 addEventListener 在某些顶级元素(甚至整个窗口)上设置侦听器并检查目标是否是锚点?您可以使用捕获侦听器来使您的侦听器在默认操作发生之前被调用。

Adding "event listeners to all the anchor tags on a page" dynamically is hard to do efficiently. You have two choices:

  1. Periodically poll the page for anchor tags (like jQuery.live() does)
  2. Use DOM mutation events (DOMNodeInserted, etc.)

The former has an obvious performance impact. Using mutation events has a hidden performance cost: if any mutation events are added for a document, Gecko takes a slower path in its DOM code, so all DOM manipulation works slower.

Why not use addEventListener to set up a listener on some top-level element (or even the whole window) and check if the target is an anchor? You could use a capturing listener to make your listener be called before the default action occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文