2016-06-08 3 views
0

'btn3'버튼이있는 크롬 확장 프로그램을 작성하려고합니다. Chrome 확장 프로그램 (popup.html)에서 해당 버튼을 클릭하면 웹 페이지의 버튼이 클릭됩니다. "일반 중등 버튼 보낼 메시지를"Chrome.tabs.executeScript - 탭이 정의되지 않았습니다.

2 질문 : 나는 chrome.tabs.executeScript은 "탭이 정의되지 않은"의 오류가

  1. 웹 페이지에있는 버튼은 다음과 같은 ID를 가지고 다음 코드에서. 어떻게 해결할 수 있습니까?
  2. content_scripts.js에 아무 것도 쓰지 않았습니까?

고마워요! 크롬 확장 프로그램 창에서 버튼의

스크립트

document.addEventListener('DOMContentLoaded', function(){ 
    document.getElementById('btn3').addEventListener('click', sendInMail) 
}); 

sendInMail = function(){ 


    chrome.tabs.executeScript(tabs[0], {file: "content_script.js"}); 

} 

content_scripts.js

alert("content_script is working!"); 

function clickSendInMailButton() { 
    var SendInMailButton = document.getElementsByClassName("regular-secondary-button send-message"), 

    SendInMailButton.click(); 
} 

clickSendInMailButton(); 

manifest.json을

{ 
    "manifest_version": 2, 

    "name": "LinkedIn Assistant", 
    "description": "This extension makes a LSS AE successful.", 
    "version": "1.0", 

    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    }, 

    "content_scripts": [ 
    { 
     "matches": ["http://*/*", "https://*/*"], 
     "js": ["content_script.js"] 
    } 
    ], 
    "chrome_url_overrides" : { 
    "newtab": "newtab.html" 
    }, 

    "background": { 
    "scripts": ["bg.js"] 
    }, 

    "permissions": [ 
    "tabs", "<all_urls>" 
    ] 


} 
+0

매니페스트에서 탭 권한을 추가하는 것 외에도이 SO 게시물 [Chromestad.executeScript가 작동하지 않음] (http://stackoverflow.com/questions)에서 제안 된대로 Chrome에서 확장 프로그램의 권한을 다시로드해야합니다./4996194/chrome-tabs-runscript-not-working)을 사용하십시오. – Teyam

답변

0
이 시도

:

sendInMail = function(){ 
    chrome.tabs.query({ 
       active: true, 
       currentWindow: true 
      }, function(tabs) { 
    chrome.tabs.executeScript(tabs[0], {file: "content_script.js"}); 

    }); 

}

+0

작동하지 않습니다. 이제 얻게 될 내용은 다음과 같습니다. extensions :: uncaught_exception_handler : 8 tabs.query에 대한 응답 오류 : 오류 : 형식 tabs.executeScript (object, object) 호출이 정의와 일치하지 않습니다 tabs.executeScript (정수 tabId, 객체 세부 사항, 선택적 콜백 함수) ... ... – adrienk

+0

Object.callback (크롬 내선 : //bdndljnkfidkacoflhfhpgilegpfgdja/sendInMail.js : 10 : 17) HTMLButtonElement.sendInMail (크롬 내선 : //bdndljnkfidkacoflhfhpgilegpfgdja/sendInMail.js : 6:15) – adrienk

+0

백그라운드 js에서이 함수를 호출합니다. – sweeta

0

the error of "tabs are not defined"

탭은 [0] - 당신은 그것을 찾을 필요가있다. 스크립트는 popup.html의 컨텍스트에서 실행됩니다. 나는 popup.html이

<head> 
... 
    <script src="popup.js"></script> 
</head> 

Script of the button in chrome extension window

popup.js에서입니다

를 포함 바랍니다. 그리고 당신은 popup.html 내부에서 코드를 실행하려고 시도하지 않습니다.

따라서이 문맥에서는 아무도 tabs[0]에 대해 알지 못합니다. 현재 어떤 창과 탭이 활성화되어 있는지 Chrome에 문의해야합니다. 그것은 ShwetaM가 쓴, 또는 수 등이 from samples

chrome.windows.getCurrent(function (currentWindow) { 
    chrome.tabs.query({ active: true, windowId: currentWindow.id }, function (activeTabs) { 
     activeTabs.map(function (tab) { 
      chrome.tabs.executeScript(tab.id, { file: 'content_script.js', allFrames: false }); 
     }); 
    }); 
}); 

또는 당신이 지정하는 경우이 경우 스크립트가 the active tab of the current window

에서 실행하기 때문에 당신은 tab.id,

chrome.tabs.executeScript({ file: 'content_script.js', allFrames: false }); 

없이 시도 할 수 있습니다 Manifest.json "content_scripts": 스크립트는 탭을 만들 때 실행되며 every refresh입니다. 확실하지는 않지만 DOM을로드하기 전에. 그러나 this content script's code will be always injected

또한 활성 탭에 찾고있는 버튼이 있는지 확인해야하기 때문에 Manifest.json에 "content_scripts":을 지정하면 안됩니다. 어떤 개체, 특히 함수의 인수에는 console.dir을 사용하십시오. 일부 텍스트의 경우 console.log; 해피 디버깅을 위해 debugger.

관련 문제