2014-02-26 21 views
2

我正在嘗試創建一個Firefox插件,它爲頁面上的電話號碼提供了超鏈接。 代碼:使用Firefox插件更新標籤內容

tabs.on('ready', function(tab) { 
    tab.attach({ 
    contentScript: 'self.postMessage(document.body.innerHTML);', 
    onMessage: function (message) { 
    html = updatePhonenumber(message);  
    } 
}); 
}); 

如何更新標籤與編輯的內容

回答

2

內容,從而實現一個使用它根據MDN docs「的網頁,其URL匹配的上下文中運行腳本PageMod的最佳方式一個給定的模式「。

所以,你將有兩個文件:

的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") 
}); 

數據/電話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); 
1

它看起來像function(tab)正在返回一個選項卡元素。如果您在瀏覽器控制檯中看到非null消息,請轉至,然後轉至tab.linkedBrowser.contentDocument.documentElement.innerHTML = 'rawr'這僅僅是一個示例,如何更改文檔,您不應該使用innerHTML,應該創建元素並附加它或刪除元素。