2014-10-17 4 views
0

Firefox 부가 기능에서 어떤 탭 메시지가 연관되어 있는지 추적해야합니다. 콘텐츠 스크립트는 main.js으로 데이터를 보냅니다. 나중에 사용자가 툴바에서 확장 프로그램의 버튼을 클릭하면 활성 탭과 관련된 데이터를 찾습니다.메시지를 보낸 탭이나 내용 스크립트를 어떻게 추적 할 수 있습니까?

Chrome 확장 프로그램에서 메시지를 받았을 때 어떤 탭에서 메시지가 왔는지 묻고 탭 ID로 메시지를 추적 할 수있었습니다. 파이어 폭스에서는 tabs도 ID를 가지고 있지만 콘텐츠 스크립트에서 쉽게 액세스 할 수있는 것 같지 않습니다.

답변

2

답변은 콘텐츠 스크립트를 만드는 방법에 따라 다릅니다. 아래 내용은 main.js 파일에 PageMod이라는 내용의 스크립트를 추가하는 파일입니다.

var buttons = require('sdk/ui/button/action'), 
    pageMod = require('sdk/page-mod'), 
    data = require('sdk/self').data; 

// Map of messages keyed by tab id 
var messages = {}; 

pageMod.PageMod({ 
    include: 'http://www.example.com', 
    contentScriptFile: [ 
    data.url('my-script.js') 
    ], 
    onAttach: function(worker){ 

    // Get the tab id from the worker 
    var tabId = worker.tab.id; 

    // Save the message 
    worker.port.on('message', function(message){ 
     messages[tabId] = message; 
    }); 

    // Delete the messages when the tab is closed 
    // to prevent a memory leak 
    worker.on('detach', function(){ 
     delete messages[tabId]; 
    }); 
    } 
}); 

var button = buttons.ActionButton({ 
    id: 'my-extension', 
    label: 'Example', 
    icon: { 
    '16': './icon-16.png', 
    '32': './icon-32.png', 
    '64': './icon-64.png' 
    }, 
    onClick: function(state){ 

    // Retrieve the message associated with the 
    // currently active tab, if there is one 
    var message = messages[tabs.activeTab.id]; 

    // Do something with the message 
    } 
}); 

는 또한 진행 방법과 상황에 적응하는 기능에 대한 더 나은 이해를 위해 Content Scripts - Interacting with Page ScriptsContent Worker을 읽어 보시기 바랍니다.