0

私はアイコンを持つ拡張機能を作成しようとしてきました。アイコンをクリックすると、(すべての)タブの読み込みが停止します。

私はこのマニフェストファイルを持っています:

{
  "name": "Stop Loading",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Stop loading all tabs in Chrome",
  "browser_action": {
    "default_icon": "greyclose.png"
  },

  "background_page": "background.html",
   "content_scripts": [ {
      "all_frames": true,
      "js": [ "kliknuto.js" ],
      "matches": [ "http://*/*", "https://*/*" ]
   } ],

  "permissions": [ "tabs", "http://*/*", "https://*/*" ]
}

background.htmlには、次のコードがあります。

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.extension.sendRequest({reqtype: "get-settings"}, function(response) {
    window.setTimeout("window.stop();", 0);
  });
});

background.htmlコードをjavascriptファイル「kliknuto.js」などに入れるべきかどうかわかりません。Chromeの拡張ボタンをクリックすると呼び出される関数はどれですか?お手数をおかけしますが、よろしくお願いいたします。

4

1 に答える 1

0

あなたのコードでは、background.htmlはそれ自体にクエリを送信し(chrome.extension.sendRequestコンテンツスクリプトには送信しません)、応答を受け取った場合、バックグラウンドページはタブではなくそれ自体window.stop()を呼び出しています。本当に必要なのは:

background.html

...
chrome.browserAction.onClicked.addListener(function(tab) {
    // get all tabs in all windows
    chrome.windows.getAll({"populate":true}, function(winArray) {
        for(var i = 0; i < winArray.length; ++i) {
            for(var j = 0; j < winArray[i].tabs.length; ++j) {
                var t = winArray[i].tabs[j];
                // push code to each tab
                chrome.tabs.executeScript(t.id, {"code":"window.stop();", "allFrames":"true"});
            }
        }
    });
});
...

このソリューションはexecuteScript、コンテンツスクリプトの代わりに使用します。

別の解決策で編集:

バックグラウンドページからの注文をリッスンする各タブにコンテンツスクリプトを添付します。

background.html

...
chrome.browserAction.onClicked.addListener(function(tab) {
    // get all tabs in all windows
    chrome.windows.getAll({"populate":true}, function(winArray) {
        for(var i = 0; i < winArray.length; ++i) {
            for(var j = 0; j < winArray[i].tabs.length; ++j) {
                var t = winArray[i].tabs[j];
                // push code to each tab
                chrome.tabs.sendRequest(t.id, {"order":"stop"});
            }
        }
    });
});
...

kliknuto.js:

chrome.extension.onRequest(function(request) {
    if(request.order == "stop") {
        window.stop();
    }
});

コンテンツスクリプトができるだけ早くリッスンを開始できるように、マニフェストのブロックに追加"run_at":"document_start"してください。content_script

于 2012-05-05T15:38:41.283 に答える