2

問題を再現する手順

a)以下のすべてのコードで拡張機能を準備します

b)https://stackoverflow.com/にアクセスします

c)「上位の質問:見出し」をクリックしますここに画像の説明を入力してください

「ポートエラー:接続を確立できませんでした。受信側が存在しません」というエラーが表示されます。

マニフェストファイル

  {
  "name": "Demo",
  "version": "1.0",
  "manifest_version": 2,
  "description": "This is a demo",

  "content_scripts": [ {
        "all_frames": true,
        "js": ["contentscript1.js" ],
        "matches": [ "https://stackoverflow.com/" ]
        }]
  }

Editor.htmlファイル

<html>
<head>
<script src="editor.js"></script>
</head>
<body>
<div id="content"></div> 
</body>
</html>

Editor.jsファイル

function onRequest(message,sender,call){ 
     document.getElementById("content").value = message; 
}
 chrome.extension.onMessage.addListener(onRequest);

contentscript1.js

function bindevent(){ 
window.open(chrome.extension.getURL("editor.html"),"_blank","toolbar=no, titlebar=no,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=720, height=400");
    message = "Sample Message"; 
    chrome.extension.sendMessage(message,function (res){}); 

}; 

document.getElementById('h-top-questions').onclick=bindevent;

助言がありますか??

4

1 に答える 1

1

子ウィンドウ/拡張ページがまだロードされている間にメッセージを送信しています。完全に読み込まれた後にメッセージを送信しましたが、問題は解決しました。

最終コード

マニフェストファイル

{
  "name": "Demo",
  "version": "1.0",
  "manifest_version": 2,
  "description": "This is a demo",

  "content_scripts": [ {
        "all_frames": true,
        "js": ["contentscript1.js" ],
        "matches": [ "http://stackoverflow.com/" ]
        }]
  }

Editor.htmlファイル

<html>
<head>
<script src="editor.js"></script>
</head>
<body>
<div id="content"></div> 
</body>
</html>

Editor.jsファイル

var message = window.location.hash.substring(1);

contentscript1.js

   function bindevent(){
 message = "Sample Message"; 
    window.open(chrome.extension.getURL("editor.html")+"#"+message,"_blank","toolbar=no, titlebar=no,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=720, height=400");
    }; 

document.getElementById('h-top-questions').onclick=bindevent

;

于 2012-11-19T12:21:37.327 に答える