2015-12-31 119 views
4

我正嘗試創建一個新擴展。我可以在後面使用chrome.runtime.sendMessage函數,但現在我已經嘗試了一切,但它仍然無法將消息發送到後臺腳本。該控制檯得到填充了content-script.js的日誌消息而不是從background.jschrome.runtime.sendMessage在Chrome擴展中不起作用

內容的script.js

console.log("Hello World!s"); 
$(document).ready(function() { 
    console.log("DOM READY!"); 
    $(document.documentElement).keydown(function (e) { 
     console.log("Key Has Been Pressed!"); 
     chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) { 
       if (response.fileData) { 
        alert("Contents Of Text File = "); 
       } 
       else { 
        console.log("No Response Received"); 
       } 
      }) 

    }) 
}); 

background.js

console.log("Atleast reached background.js") 
     chrome.runtime.onMessage.addListener (
      function (request, sender, sendResponse) { 
       console.log("Reached Background.js"); 
       if (request.Message == "getTextFile") { 
        console.log("Entered IF Block"); 
         $.get("http://localhost:8000/quicklyusercannedspeechbucket/helloWorld1", function(response) { 
        console.log(response); 
        sendResponse({fileData: response}) 
       }) 
      } 
      else { 
       console.log("Did not receive the response!!!") 
      } 
     } 
    ); 

清單。 json

{ 
    "manifest_version": 2, 
    "name": "My Cool Extension", 
    "version": "0.1", 
    "content_scripts": [ { 
    "all_frames": true, 
    "js": [ "jquery-2.1.4.min.js", "content-script.js" ], 
    "matches": [ "http://*/*", "https://*/*", "file://*/*" ] 
    } ], 
    "permissions": [ "http://*/*", "https://*/*", "storage" ], 
    "background": { 
    "scripts": [ 
     "jquery-2.1.4.min.js", 
     "background.js" 
    ] 
    } 
} 

任何幫助表示讚賞:)

謝謝!

回答

2

你需要改變你的代碼,以便在background.js你必須改變行爲:雖然在contentscript你需要做的

console.log("Atleast reached background.js") 
chrome.runtime.onMessage.addListener (
    function (request, sender, sendResponse) { 
     console.log("Reached Background.js"); 
     if (request.Message == "getTextFile") { 
      console.log("Entered IF Block"); 
      $.get("http://localhost:63342/Projects/StackOverflow/ChromeEXT/helloWorld1", function(response) { 
       console.log(response); 

       // to send back your response to the current tab 
       chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 
        chrome.tabs.sendMessage(tabs[0].id, {fileData: response}, function(response) { 
         ; 
        }); 
       }); 


      }) 
     } 
     else { 
      console.log("Did not receive the response!!!") 
     } 
    } 
); 

console.log("Hello World!s"); 
$(document).ready(function() { 
    console.log("DOM READY!"); 
    $(document.documentElement).keydown(function (e) { 
     console.log("Key Has Been Pressed!"); 
     chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) { 
      ; 
     }) 

    }) 
}); 


// accept messages from background 
chrome.runtime.onMessage.addListener (function (request, sender, sendResponse) { 
    alert("Contents Of Text File = " + request.fileData); 
}); 

sendResponse可以作爲即時反饋而不是計算結果。

+0

就是這樣!它像一個魅力。現在,你能解釋一下爲什麼你使用''chrome.tabs''?我無法正確理解代碼示例中的數據流。是這樣嗎? 內容腳本(生成事件) - >後臺腳本(進程事件) - >後臺腳本(創建響應事件) - >內容腳本(進程響應事件)? 難道沒有其他方法可以做到嗎?抱歉挑剔,但由Google提供的文檔不好:( –

+1

@PranavJituri通常我會寫擴展名,以便我的背景只與當前標籤進行通信,如果此標籤符合我的興趣,否則我不注入jquery,js,。 ...我不惜一切代價來重載Chrome瀏覽器,是的,您正確理解了我的流程,並且我建議您查看Chrome擴展程序[Message Passing](https://developer.chrome.com/extensions/messaging) – gaetanoM

+0

非常感謝你幫忙解釋代碼背後的概念:) –