我正在尝试创建一个 Firefox 插件,它可以提供指向页面上电话号码的超链接。 代码:
tabs.on('ready', function(tab) {
tab.attach({
contentScript: 'self.postMessage(document.body.innerHTML);',
onMessage: function (message) {
html = updatePhonenumber(message);
}
});
});
如何使用已编辑的内容更新选项卡的内容
最佳答案
实现这一目标的最佳方法是使用 PageMod根据MDN docs “在 URL 与给定模式匹配的网页上下文中运行脚本”。
因此,您将有两个文件:
lib/main.js:
var pageMod = require("sdk/page-mod");
var self = require("sdk/self");
pageMod.PageMod({
include: "*", // <- this tell firefox to attach the following
// content script to all web pages
contentScriptFile: self.data.url("phone-detector.js")
});
data/phone- detector.js:
/* This is a normal js file that gets attached to the current web page
and has access to the DOM*/
var updatePhonenumber = function(dom) {
// do whatever you should do
}:
updatePhonenumber(document.body.innerHTML);
关于javascript - 使用 Firefox 插件更新选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22055621/