2013-06-18 12 views
5

私はこのシンプルな拡張機能を持っています。これはクロームのツールバーにアイコンを表示し、有効/無効ボタンを表示します。私が訪問し、Googleのウェブサイトに発射content_script.jsを無効または有効にボタンに機能を追加したい:Google拡張機能のcontent_scriptのオン/オフを切り替えるにはどうすればよいですか?

popup.js

var setLayout = function(){ 
    var list = document.createElement('ul'); 

    var enable = document.createElement('li'); 
    enable.appendChild(document.createTextNode('Enable Script')); 
    enable.onclick = function(){toggle(0)}; 

    var disable = document.createElement('li'); 
    disable.appendChild(document.createTextNode('Disable Script')); 
    disable.onclick = function(){toggle(1)}; 

    list.appendChild(disable); 
    document.body.appendChild(list); 

    function toggle(n){ 
     list.removeChild(n == 0 ? enable : disable); 
     list.appendChild(n == 0 ? disable : enable); 
    } 
}; 

document.addEventListener('DOMContentLoaded', setLayout, false); 

manifest.jsonを

{ 
    "manifest_version": 2, 

    "name": "test", 
    "description": "test", 
    "version": "1.0", 

    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    }, 
    "content_scripts": [ 
    { 
     "matches": ["https://www.google.co.uk/"], 
     "js": ["content_scripts.js"] 
    } 
    ] 
} 

content_scripts.js

(function(){ 
    alert('hello'); 
})(); 

Google拡張機能の新機能です私は、無効/有効にするボタンをクリックした後、マニフェストを変更することを考えましたが、Googleのウェブサイト上のドキュメントを読んだ後に正しいコマンドを見つけることができませんでした!

お手数をおかけしますようお願い申し上げます。あなたは、コンテンツのスクリプトを挿入するかどうかを決定するために背景ページとコンテンツのスクリプト間message passingを使用することができます

chrome.tabs.executeScript(null, {file: "content_script.js"}); 

、または:

答えて

2

私はいくつかの研究の末、backround pagessendMessageおよびlocalstorageを使ってこの問題を解決する方法を見つけました。

background pagesは、popup.jscontent_scripts.jsの間のコミュニケータとして動作しますが、2つの異なるドキュメントにあり、それらの間で変数を直接渡すことはできません。

私は追加mamifestで背景ページを有効にするには:ローカル変数保存

"background": { 
"scripts": ["background.js"], 
"persistent": true 
}, 

localstorageをし、ブラウザを閉じた場合でも、それらを覚えて、再び開くので、有効/無効ボタンは、それがどのできるlocalStorage['status'] = 1/0を設定クリックされたときに、 background.jsからアクセスし、content_scripts.jsに渡します。要求を待ち受けbackground.jsと上onMessage

if(!localStorage.status) localStorage['status'] = 1; 
toggle(2); 
enable.onclick = function(){toggle(0)}; 
disable.onclick = function(){toggle(1)}; 
function toggle(n){ 
    if((n == 0) && (enable.parentNode == list)){ 
     list.removeChild(enable); 
     list.appendChild(disable); 
     localStorage.status = 1; 
    }else if((n == 1) && (disable.parentNode == list)){ 
     list.removeChild(disable); 
     list.appendChild(enable); 
     localStorage.status = 0; 
    }else if((n == 2) && (!list.hasChildNodes())){ 
     list.appendChild((localStorage.status == 1) ? disable : enable); 
     chrome.browserAction.setIcon({path: (localStorage.status == 1) ? "icons/icon19.png" : "icons/icon19_disabled.png"}); 
    }else{ 
     return; 
    } 
} 

iはcontent_scrips.jsロードされた送信要求メッセージのbackground.js上へsendMessageを使用していたlocalStorage.statuscontent_scripts.jsに渡す、と:

のlocalStorage変数を設定するには、私はpopup.jsに追加しましたlocalStorage.statusの値を使用してcontent_scripts.jsに返信してください。

background.js:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) { 
    if (request.type == "status") sendResponse({status: localStorage.status}); 
}); 

content_scripts。js

var fn = function(){...}; 
chrome.runtime.sendMessage({type: "status"}, function(response) { 
    if(response.status == 1) fn(); 
    return; 
}); 

これは、誰かが役に立つと願っています。

1

は、マニフェストファイルではなく、このようなものをコードを使用してinjecting the content scriptお試しくださいおそらく、拡張機能のアクションボタンで設定されたフラグが実行されている場合にのみ実行されるコンテンツスクリプト自体のif文です。

browser action eventを使用して、アクションボタンが押されたときを検出できます。

+0

あなたの答えに感謝しますが、私はGoogleの拡張機能にはまったく新しいです。あなたの答えをもっと詳しく説明したり、自分のソリューションに自分のコードを適用する方法がわかっていれば、 – razzak

関連する問題