[英]Update tab content using firefox addon
我正在嘗試創建一個Firefox插件,該插件可為頁面上的電話號碼提供超鏈接。 碼:
tabs.on('ready', function(tab) {
tab.attach({
contentScript: 'self.postMessage(document.body.innerHTML);',
onMessage: function (message) {
html = updatePhonenumber(message);
}
});
});
如何使用已編輯的內容更新選項卡的內容
實現這一目標的最佳方法是使用PageMod ,根據MDN文檔, “在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);
看起來function(tab)
返回一個tab元素。 如果在瀏覽器控制台中看到非null
消息,請嘗試進入console.log(tab.linkedBrowser)
,然后進入tab.linkedBrowser.contentDocument.documentElement.innerHTML = 'rawr'
這只是一個如何更改文檔的示例,您不應該使用innerHTML ,應創建元素並將其追加或刪除元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.