3

私は、Google Chrome のコンテンツ スクリプト拡張機能を作成しています。これは、Web サイトのページに機能を追加します。いくつかのオプションを追加したいのですが、実際には大したことではありません.2つの文字列が必要です(いずれも機密ユーザーデータではありません)。

この回答から、拡張機能に追加したくないバックグラウンドページが必要だと思います-不要な重量を増やしたくありません。

バックグラウンド ページは本当に必要ですか、それともバックグラウンド ページなしでオプション ページを作成できますか (また、どのストレージを使用できますか)。

4

2 に答える 2

2

更新
Chrome 20以降、Storage APIを使用できるようになりました.....
http://code.google.com/chrome/extensions/storage.html

古い方法
私がしていることは、ローカルストレージから必要な設定を取得するスクリプトを持つ拡張機能のページを指すiframeを作成し、それをコンテンツスクリプトが取得するメッセージで親に送信することです.... .まあ、それはがらくたの説明でした、コードはそれをよりよく言います;).......

コンテンツ スクリプト

// create the iframe for our page that sends the settings
var el = document.createElement("iframe");
el.setAttribute('src', chrome.extension.getURL("gimmeSettings.html"));
el.style.visibility="hidden";
document.body.appendChild(el);

// create the listner that listens for a message from our page that sends the settings
window.addEventListener("message", receiveSettings, false);

// function that gets called when we recieve a message from the page that sends the settings  
function receiveSettings(event) {
        //check to make sure the message came from our page
        if (event.origin !== "chrome-extension://"+chrome.i18n.getMessage("@@extension_id")) return;

        //message came from our extension, do stuff with it  
        console.debug(event.data);

        // clean up
        window.removeEventListener("message", receiveSettings, false);
        el.parentNode.removeChild(el);
}

gimmeSettings.html の JS

// post the message with our settings
parent.postMessage( localStorage.getItem("testing"), "*" );

Options.html の JS

localStorage.setItem("testing","bleh");

マニフェスト

{
    "name": "Getting at an extensions local storage from a content script",
    "description" : "Getting at an extensions local storage from a content script.  Be aware that other pages/extensions can use this to get at your settings, but not change them...so dont include sensitvie data.",
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js" : ["myscript.js"],
            "run_at":"document_idle"
        }
    ],
    "permissions": [
        "tabs", "<all_urls>"
    ],
    "manifest_version": 2,
    "web_accessible_resources": [
    "gimmeSettings.html"
  ],
  "options_page": "options.html",
    "version":"1.0"
}

注意すべき点....
他のページや拡張機能は、これを使用して拡張機能から設定を簡単に取得できるため、この方法で機密データを使用しないでください。
私が知る限り、彼らがそのページからあなたの設定を変更する方法はありません.誰かが違うことを知っているなら、説明してください.
私はマニフェスト バージョン 2 を使用しており、ページ gimmeSettings にアクセスできるように設定しています。マニフェスト バージョン 2 の違いがわからない場合は、実際に読んで ください。 http://code.google.com/chrome/extensions/trunk/manifestVersion.html

実際の例が必要な場合は、ここにアクセスしてください.....
http://forum.valorsolo.com/viewtopic.php?f=36&t=375

于 2012-03-06T07:58:57.733 に答える
0

アイデアは思いつきましたが、それが適切かどうか、または理にかなっているのかどうかはわかりません。

localStorageからHTML5 にアクセスしcontent_script、そこに 2 つの文字列を保存できると思います。メッセージ パッシングを介して、(オプション ページから) それらの 1 つが変更されたことを に伝え、content_scriptそれを更新させることができるはずlocalStorageです。

これが唯一の選択肢でしょうか?それが機能するなら、それほど悪くはないように聞こえます...

于 2012-01-27T13:33:30.407 に答える