简体   繁体   English

如何通过Firefox扩展/附加组件将按钮添加到任何网站?

[英]How to add a button to any website through firefox extension / add-on?

I would like to create an extension that searches the elements on a page, finds a specific element (ex. Facebook like button) and adds my own button next to it. 我想创建一个扩展来搜索页面上的元素,找到一个特定的元素(例如Facebook之类的按钮),并在其旁边添加我自己的按钮。 Is this possible and if so how do I go about implementing it? 这可能吗?如果可以,我该如何实施呢?

UPDATE: So I tried to implement the feature I want using: 更新:因此,我尝试实现要使用的功能:

onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget; // doc is document that triggered "onload" event

    var fblike = document.getElementsByTagName("fb-root");
    var button = top.window.content.document.createElement("input");
    button.setAttribute("type", "button");
    button.setAttribute("value", "valore");
    button.setAttribute("name", "nome");
    var parentDiv = fblike.parentNode;
    parentDiv.insertBefore(button, fblike);
    alert(button);
},

I still cannot get the button to even show up on the page. 我仍然无法获得该按钮甚至无法显示在页面上。 Is there anyway to get the button to show just on top of the whole page so I know the problem is finding the 'fb-root' tag? 无论如何,有没有让该按钮显示在整个页面顶部的按钮,所以我知道问题出在找'fb-root'标签吗? I am trying to detect a Facebook like button. 我正在尝试检测Facebook之类的按钮。

Finding a specific element on a page in Javascript is pretty trivial - if the element has its own id then you just use document.getElementById(someId) . 用Javascript在页面上查找特定元素非常简单-如果元素具有自己的id则只需使用document.getElementById(someId) Adding an element to the DOM is similarly easy (and this all gets even easier using frameworks like jQuery). 向DOM添加元素也同样容易(使用jQuery之类的框架,这一切都变得更加容易)。 However, I'm afraid I have no experience writing browser extensions. 但是,恐怕我没有编写浏览器扩展的经验。 But I can tell you yes, this is definitely possible. 但是我可以告诉你,这绝对是可能的。 :) :)

When you work with the page's document you should do so consistently - don't use document because that will refer to the document you are running in (typically the browser window). 处理页面的文档时,应始终保持一致-请勿使用document因为这将引用您在其中运行的文档(通常是浏览器窗口)。 You should also make sure to create the node in the same document where you will be inserting it (and don't forget to check Error Console, then you should be able to say something more differentiated than "doesn't work"). 您还应该确保在将要插入该节点的同一文档中创建该节点(并且不要忘记检查错误控制台,这时您应该能够说出比“不起作用”更具区别性的内容)。 Your code with mistakes fixed: 您的代码已修正错误:

var fblike = doc.getElementsByTagName("fb-root");
var button = doc.createElement("input");
button.setAttribute("type", "button");
button.setAttribute("value", "valore");
button.setAttribute("name", "nome");
var parentDiv = fblike.parentNode;
parentDiv.insertBefore(button, fblike);

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

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