0

Chrome 拡張機能をマニフェスト 2 に移行しようとしています。現在のバージョンでは、基本的に 1 つの大きなスクリプト タグである background.html ページを使用しています。それができなくなったので、background.js スクリプトの使用に切り替え、多くの検索と実験を行った後も、外部ファイルからスクリプトを挿入できませんでした。現在のバージョンでは、document.write を使用して、ブラウザーの読み込み時に実行されるスクリプト タグを挿入するだけですが、これを行う方法が今のところ見つかりません。

chrome.tabs.onUpdated.addListener 関数と、XMLHttpRequest オブジェクトを使用してタブ更新ごとにスクリプトを挿入する機能を認識していますが、実行したいスクリプトは、ブラウザーが読み込まれたときにのみ実行する必要があります。

現在のコード (background.html ファイルの script タグ内):

document.write("<script type='text/javascript' src='" + url + "'></script>");

background.js ファイルでは、これにより次のエラーが発生します: スクリプト 'http://www.mydomain.com/script.js' の読み込みを拒否されました。 chrome-extension-resource:".

以下を含むあらゆる種類のコードを試しました。

var s = document.createElement('script');
s.src = chrome.extension.getURL(url);
s.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);

しかし、これはローカルスクリプトファイルに対してのみ機能するようですが、外部オンラインファイルをロードする必要があります。

4

2 に答える 2

1

あなたが得ているエラーは、マニフェストのディレクティブcontent_security_policyによるものです。スクリプトのドメインをその一部として指定する必要があります。jQuery の例を次に示します。

"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"

そして Background.html ページで

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

これにより、エラーは発生しませんでした。このスクリプトをいつダウンロードするかは、拡張機能のアーキテクチャ次第ですが、アプリケーションにその方法を伝える方法はたくさんあります。アプリケーションが最初にロードするのはバックグラウンド ページであるため、ロード時にバックグラウンド ページからコンテンツ スクリプトにメッセージを送信できます。例:

/**
* Listener for Displaying the Extension Page Action when the Tab is updated.
* @private
* @event displayPageAction
* @param {Number} tabId The tabId given by the tabs listener to know which tab was updated.
* @param {Object} changeInfo The current status of the tab.
* @param {Object} tab The metadata of the tab.
**/
var displayPageAction = function (tabId, changeInfo, tab) {
    var match = regexAIESEC.exec(tab.url); // var regexAIESEC = new RegExp(/http:\/\/www.myaiesec.net\//);
    // We only display the Page Action if we are inside a MyAIESEC Tab.
    if(match && changeInfo.status == 'complete') {
        //We send the proper information to the content script to render our app.
         chrome.tabs.sendMessage(tab.id, {load: true}, function(response) {
            if(response) {
                //After successfully getting the response, we show the Page Action Icon.
                chrome.pageAction.show(tab.id);     
            }
        });
    }
};

これは、タブが更新された後に Page アクションを表示する場合の例です。前のコードでは:

chrome.tabs.onUpdated.addListener(displayPageAction);

Chrome 拡張機能のコンテンツ セキュリティ ポリシーの詳細についてはこちらを、メッセージ パッシングの詳細についてはこちらをご覧ください

于 2012-12-18T18:27:51.710 に答える
0

a)バックグラウンド ページのドキュメントである ため、document.write("<script type='text/javascript' src='" + url + "'></script>");動作しません。ドキュメントアーキテクチャを参照してください。background.jsdocument

b)chrome.extension.getURL(url);ファイルの相対 URL を取得します。代わりにタブ APIを試すことができます

次のコードを使用して、任意のページにJqueryを挿入できました。権限が正しく設定されていますか?

参考文献

a)コンテンツ スクリプト

b)マニフェスト バージョン 2

c)タブ API

d)背景ページ

マニフェスト.json

{
"name":"Script Injection Demo",
"description":"This demonstrates Script Injection",
"version":"1",
"manifest_version":2,
"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["myscript.js"]
    }
  ]
}

myscript.js

//Creating a Script Tag
var _scriptTag = document.createElement('script');
//Referencing external URL
_scriptTag.src='https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js';
//Appending child
(document.head||document.documentElement).appendChild(_scriptTag);

この種の注射が効かない場合はお知らせください。

于 2012-12-18T18:15:53.247 に答える