2017-07-13 5 views
0

SendMessageは新しいタブコンソールでは機能しません。ポップアップからローカルindex.htmlで新しいタブを開き、メッセージを送信します

メッセージが開いたばかりのタブにメッセージが届きません。

コンソールに何も表示されません。以下は

私は

Manisfest.json

{ 
    "name": "SYSHP", 
    "version": "1.0", 
    "description" : "SYSHP", 
    "manifest_version": 2, 
    "permissions": [ "http://*/", "tabs", "activeTab", "notifications", "background" ], 
    "background": { 
     "scripts": [ 
      "background.js" 
     ] 
    }, 
    "content_scripts": [ 
     { 
      "matches": [ 
       "<all_urls>" 
      ], 
      "js": [ 
       "content.js", 
       "lib/jquery.min.js" 
      ], 
      "run_at": "document_end" 
     } 
    ], 

    "icons": { 
    "128": "icon128.png", 
    "48": "icon48.png" 
    }, 

    "browser_action": { 
     "default_icon": "icon128.png", 
     "default_title": "SYSHP", 
     "default_popup": "popup.html" 
    } 

} 

popup.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
    <title>SYSHP</title> 
</head> 
<body> 

    <button id="preparaLista">Preparar Lista</button> 
    <button>Download</button> 


</body> 

<script src="lib/jquery.min.js"></script> 
<script src="popup.js"></script> 

</html> 

popup.js

var url = ('chrome-extension://' + chrome.i18n.getMessage('@@extension_id') + '/index.html'); 

document.getElementById("preparaLista").addEventListener("click",handleClick); 

    function handleClick(){ 
     chrome.tabs.create({ url: url }, function(tab) { 
      chrome.tabs.sendMessage(tab.id, {type: "action_example"}); 
     }); 
    } 

インデックスを使用していますフォーマット内のすべてのファイルです.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 

    <title>SYSHP</title> 
</head> 
<body> 




</body> 
<script src="lib/jquery.min.js"></script> 
<script src="content.js"></script> 


</html> 

content.js

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) { 
    console.log(sender.tab ? 
       "from a content script:" + sender.tab.url : 
       "from the extension"); 

    console.log("received message from popup: "+request.type); 

}); 
+0

こんにちは、タブが実行されているが、メッセージが送信されていません。 –

+0

これは通常のファイルをロードしています。しかし、メッセージはコンソールに表示されません。 tks –

+0

*あなたが**必要な場合を除いて**すべての**ページ(あなたの 'matches'で' content_scripts')にjQueryをロードしないでください。 jQueryは最小化されたコードの85キロバイトです。これは、すべての単一ページ*に鞍を打つことに重大な負担です。タブが100個開いている私たちの何人か? jQueryをロードするには本当に必要なのですが、バニラJavaScriptを使用しないことで、あなた自身のコードに数百/数百文字を保存する便利性のためにそうする可能性が高くなります。そのような場合(私たちは知る方法がありません)、そうすることは、あなたのユーザの視点からは非常に貧弱なトレードオフです。 – Makyen

答えて

0

popup.js

document.getElementById("preparaLista").addEventListener("click",handleClick); 

    function handleClick(){ 
     chrome.tabs.create({ url: chrome.runtime.getURL("index.html") }, function(tab) { 

      var handler = function(tabId, changeInfo) { 
       if(tabId === tab.id && changeInfo.status === "complete"){ 
       chrome.tabs.onUpdated.removeListener(handler); 
       chrome.tabs.sendMessage(tabId, {type: 'opa'}); 
       } 
      }; 

      // in case we're faster than page load (usually): 
      chrome.tabs.onUpdated.addListener(handler); 
      // just in case we're too late with the listener: 
      chrome.tabs.sendMessage(tab.id, {type: "action_example2"}); 

     }); 
    } 

content.js

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) { 
    console.log(sender.tab ? 
       "from a content script:" + sender.tab.url : 
       "from the extension"); 

    console.log("received message from popup: "+request.type); 

}); 
+0

これは実際の回答ですか、それともあなたの質問に関する追加情報ですか? – Makyen

+0

このコードは問題を解決するかもしれませんが、これが問題を解決する方法と理由を本当に助けます(http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)。あなたの投稿の質を向上させる。あなたが今質問している人だけでなく、将来読者のための質問に答えていることを忘れないでください!説明を追加するためにあなたの答えを編集し、どのような制限と前提が適用されるかを示してください。 – Makyen

関連する問題