2014-02-26 5 views
2

페이지의 전화 번호에 하이퍼 링크를 제공하는 Firefox 추가 기능을 만들려고합니다. 코드 :firefox addon을 사용하여 탭 내용 업데이트

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)이 탭 요소를 반환하고 있습니다. 시도 console.log(tab.linkedBrowser)null 브라우저 콘솔에 메시지가 표시되면 tab.linkedBrowser.contentDocument.documentElement.innerHTML = 'rawr'이 문서를 변경하는 방법에 대한 예입니다. innerHTML을 사용하면 안되며, 요소를 만들고 추가하거나 요소를 제거해야합니다.

관련 문제