2017-01-31 24 views
0

我在製作爛番茄擴展程序,如果您輸入電影名稱,它將顯示評論者/觀衆得分。現在,我試圖讓我的後臺腳本與內容腳本進行通信,以操縱打開的電影選項卡的DOM。存在一個小問題:內容js只能在收聽活動選項卡上設置的消息時打印到屏幕上。Chrome擴展程序:如何在非活動選項卡上激活內容腳本

編輯:只是爲了澄清我上面寫了什麼,我試圖從非活動選項卡刮取數據。到目前爲止,我只能獲得script.js在活動選項卡中進行響應。我推斷,爲了用chrome api刮一個頁面,它必須在一個tab中打開;但是,我不希望打開的選項卡中斷用戶的網上衝浪,因此無法使用選項卡。如果有人有更簡單的方法去做這件事,我就會全神貫注。

background.js(這工作)

chrome.runtime.onMessage.addListener(
    function(request,sender,sendResponse){ 
     if(request.message==="clicked_browser_action"){ 

      chrome.tabs.query({"active":true, "currentWindow":true},function(tabs){ 
       console.log(tabs) 
       var activeTab = tabs[0]; 
       console.log(activeTab) 
       chrome.tabs.sendMessage(activeTab.id, {"message":"message2Tab"}) 
      }) 
     } 
    }) 

但不是這樣的:

chrome.runtime.onMessage.addListener(
    function(request,sender,sendResponse){ 
     if(request.message==="clicked_browser_action"){ 

      chrome.tabs.query({"currentWindow":true},function(tabs){ 
       var activeTab = tabs[tabs.length-1]; //find last tab 
       console.log(activeTab) 
       chrome.tabs.sendMessage(activeTab.id, {"message":"message2Tab"}) 
      }) 
     } 
    }) 

content.js:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) { 
    console.log('started') 
    if(request.message === "message2Tab") { 
    console.log('started') 

    } 
    } 
); 

popup.html

<!DOCTYPE html> 
<script src="jquery-3.1.1.min.js"></script> 
<script src="popup.js"></script> 
Search RT for Movie: <input type="text" value=""><input type="submit" id="rt"> 
</div> 

的manifest.json

{ 
    "manifest_version":2, 
    "name":"extension", 
    "version":"0.1", 
    "content_scripts":[ 
     { 
      "matches": [ 
       "<all_urls>" 
      ], 
      "js":["content.js"] 
     }], 
    "browser_action":{ 
       "default_icon":"icon.png", 
       "default_popup":"popup.html" 
      } 
     , 
    "background":{ 
     "scripts":["background.js"] 
    }, 
    "permissions":[ 
     "tabs" 
    ] 

} 

我張貼在earlier SO question完整的代碼。對你的幫助非常感謝到目前爲止

+0

我不需要看控制檯輸出,我只是想從該頁面檢索文本 – st4rgut

+0

請[編輯]題目:包括一個**完整** [mcve] *重複的問題*。包括* manifest.json *,一些背景/內容/彈出腳本/ HTML。尋求調試幫助的問題(「**爲什麼不是這個代碼工作?」)必須包括:►期望的行爲,►特定問題或錯誤*和*►在問題中重現問題所需的最短代碼**本身**。沒有明確問題陳述的問題對其他讀者無益。請參閱:「**如何創建[mcve] **」,[我可以在此處詢問哪些主題?](http://stackoverflow.com/help/on-topic)和[問]。 – Makyen

+0

好的,我更新了我的問題,以防不清楚我在 – st4rgut

回答

0

我用鉻存儲選項卡之間進行通信:

content.js

chrome.storage.local.set({"keyName": [critics,audience,title]},function(){console.log('mah critics stored')}); 

popup.js

chrome.storage.local.get("keyName",function(items) { 
     console.log(items) 
     console.log(items.keyName) 
     var criticsscore = items.keyName[0] 
     var audiencescore = items.keyName[1] 
     $('#criticsscore').html('Critics ' + criticsscore) 
     $('#audiencescore').html('Audience ' + audiencescore) 
     $('#title').html(items.keyName[2]) 
    // Do something with items.keyName 
}); 
});  

    chrome.storage.local.get("keyName",function(items) {//initialize the application 
     console.log(items) 
     console.log(items.keyName) 
     var critics = items.keyName[0] 
     var people = items.keyName[1] 
     $('#criticsscore').html('Critics ' + critics) 
     $('#audiencescore').html('Audience ' + people) 
     $('#title').html(items.keyName[2]) 

    // Do something with items.keyName 
}); 
相關問題