2016-06-08 37 views
0

我正在嘗試編寫一個名爲'btn3'的按鈕的Chrome擴展。當我點擊Chrome擴展程序中的那個按鈕(popup.html)時,它會點擊網頁上的一個按鈕。網頁上的按鈕有以下標識:「常規輔助鍵發送消息」Chrome.tabs.executeScript - 製表符未定義

2個問題:

  1. 我得到的「標籤沒有定義」的錯誤chrome.tabs.executeScript在下面的代碼中。我該如何解決這個問題?
  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帖子中的建議重新加載擴展程序的權限 - [chrome.tabs.executeScript not working](http://stackoverflow.com/questions/4996194 /鉻標籤 - executescript - 不工作)。 – 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,對象細節,可選在HTMLButtonElement.sendInMail(鉻擴展17) :回調函數)... ... – adrienk

+0

在 Object.callback(鉻擴展://bdndljnkfidkacoflhfhpgilegpfgdja/sendInMail.js:10 //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 }); 
     }); 
    }); 
}); 

,或者你可以嘗試沒有tab.id,

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

,因爲在這種情況下,該腳本將在the active tab of the current window

運行當在指定Manifest.json "content_scripts":腳本將在您創建制表符和every refresh時執行。我不確定,但也許在加載DOM之前。但是,您不應在Manifest.json中指定"content_scripts":,因爲this content script's code will be always injected

此外,您必須確保活動選項卡包含您正在查找的按鈕。對某些對象使用console.dir,特別是函數中的參數; console.log一些文字; debugger用於快速調試。