2013-06-18 2 views
5

이 간단한 확장 기능을 사용하면 크롬 도구 모음에 아이콘이 표시되고 사용/사용 안 함 버튼이 표시됩니다. 내가 비활성화하거나 구글 웹 사이트 방문시 기록 content_script.js 수 있도록 버튼에 기능을 추가 할 :Google 확장 프로그램의 content_scripts를 사용/사용 중지하려면 어떻게해야하나요?

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 pages, sendMessagelocalstorage을 사용하여이 문제를 해결하는 방법을 알아 냈습니다.

background pagespopup.jscontent_scripts.js 사이의 커뮤니케이터로 작동하며, 서로 다른 두 문서에 있으며 직접 변수를 전달할 수 없습니다.

내가 추가 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; 
    } 
} 

내가 content_scrips.js로드 전송 요청 메시지에 background.js에게 sendMessage을 사용했다 localStorage.statuscontent_scripts.js에 전달하고 :

로컬 스토리지 변수를 설정하려면 내가 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

관련 문제