如何从 Firefox 扩展向网页添加元素
我将开发一个 Firefox 扩展,当找到标签 并找到文件时,它应该在加载的页面中放置一个按钮已选择。
同样,我认为 Skype 工具栏也做了类似的事情:当网站包含电话号码时,Skype 扩展程序会自动将其转换为可以单击以呼叫 Skype 联系人的按钮。
我使用的是 GNU/Linux 系统,不幸的是 Skype 扩展在 Linux 版本的 Firefox/Skype 上不起作用,所以我什至无法尝试对任何内容进行逆向工程...
Firefox 扩展包含文件 overlay.js:该文件包含扩展的主要逻辑。 在这里,我可以简单地使用以下代码找到 节点:
onFileChosen: function(aEvent) {
var input = aEvent.explicitOriginalTarget;
if(input.type=="file"){
alert(input.value); }
}
window.addEventListener("change", function(e) {xpitest.onFileChosen(e)},false);
因此,当选择文件时,会出现一个警报窗口并显示文件名。
但是,选择文件后如何在页面中放置按钮?
我一直在尝试使用各种 document.parentNode 和类似的东西,但似乎没有任何效果。
或者我是否有可能无法将内容放入加载的页面中?
谢谢
I'm going to develop a Firefox extension which should put a button in the loaded pages, when the tag: <input type="file" ... >
is found and a file has been selected.
Likewise, I think the skype toolbar does a similar thing: when a website contains a phone number, the Skype extension automatically converts it into a button that can be clicked to call skype contacts.
I'm on a GNU/Linux system, and unfortunately the skype extension does not work on Linux versions of Firefox/skype, so I can't even try to reverse engineer anything...
The firefox extension contains the file overlay.js: this file contains the main logic for the extension. Here I can find <input type="file" ... >
nodes simply with this code:
onFileChosen: function(aEvent) {
var input = aEvent.explicitOriginalTarget;
if(input.type=="file"){
alert(input.value); }
}
window.addEventListener("change", function(e) {xpitest.onFileChosen(e)},false);
So, when a file has been chosen, an alert window appears and shows the file name.
But, how can I put a button in the page when a file has been chosen?
I've been trying with the various document.parentNode and similars, but nothing seems to work.
Or is it possible that I can't put stuff into the loaded page?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 chrome 上下文中,您可以使用 top.window.content.document 获取当前内容文档(例如带有文件选择器的页面)。 此时,就像你的 JS 正在页面上运行一样。 如果这没有帮助,请发布您的代码并提供尽可能多的信息。 另请参阅在 Chrome 代码中使用窗口。
你绝对可以将东西注入到页面中。
From the chrome context, you can get the current content document (e.g. the page with the file chooser) using top.window.content.document . At that point, it's just like your JS is running on the page. If that doesn't help, please post your code with as much info as possible. See also Working with windows in chrome code.
And you definitely can inject things into the page.