2017-09-15 126 views
0

我正在構建Chrome擴展,當點擊工具欄圖標時,我正在執行background.js,並在background.js我正在運行執行腳本。就像這樣:通過background.js訪問網站變量與注入腳本

chrome.tabs.executeScript(null, { file: "libs/jquery.min.js" }, function() { 
    chrome.tabs.executeScript(null, { file: "scripts/myScript.js" }) 
}); 

myScript.js,我可以證實,它已執行jQuery和運行我的腳本了。


myScript.js:

console.log('test'); 

$(document).ready(function() { 
    console.log('jQuery test'); 
}) 

他們都工作


在這一點上,我想實現一個變量,它是網頁(不是我寫的內);假設它是var helloWorld = "the content"

如果我嘗試在控制檯(開發工具),console.log(helloWorld),它的工作原理。不過,如果我嘗試在myScript.js,它說:

$(document).ready(function() { 
    console.log(helloWorld) 

    setTimeout(function() { 
     console.log(helloWorld) 
    }, 1500) 
}) 

Uncaught ReferenceError: helloWorld is not defined


我已經看到了一些解決方法爲內容腳本的做法,但我不能使它成爲executeScript方式工作。如何訪問myScript.js中的helloWorld變量?


我試過使用捕食者的方法。下面是代碼:

injected.js:

chrome.runtime.sendMessage("njfhnpgecmbmbipcjpkkglbkpcbjammb", {varName: "test"}); 
// I tried returning a string first 

background.js

chrome.tabs.executeScript(null, { file: "libs/jquery.min.js" }, function() { 
    chrome.tabs.executeScript(null, { file: "scripts/myScript.js" }) 
}); 

chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse){ 
    console.log(message.varName); 
    if(message.varName !== undefined){ 
     console.log(message.varName); 
    } 
}); 

清單:

"background": { 
    "scripts": ["scripts/background.js"], 
    "persistent": true 
}, 
根據您的第一個答案

myScript.js

根據更新後的答案10

myScript.js

function exec(fn) { 
    var script = document.createElement('script'); 
    script.setAttribute("type", "application/javascript"); 
    script.textContent = '(' + fn + ')();'; 
    document.body.appendChild(script); //run the script 
    document.body.removeChild(script); //clean up 
} 

background.js沒有趕上

+0

我建議你閱讀[消息傳遞(https://developer.chrome.com/extensions/messaging)。 – PredatorIWD

+0

如何讀取第三方網站DOM上的數據,以便我可以使用消息傳遞?我之前使用Message Passing在腳本之間進行通信(background.js和content_script.js),但我堅持問題的第一部分(在第三方網站DOM上閱讀js變量) – senty

+0

您注入內容腳本,獲取變量然後將其發送到您的後臺腳本進行處理。 – PredatorIWD

回答

0

您的內容腳本是在一個單獨的環境中運行的消息,這意味着它不能訪問網頁的全局範圍。但是我沒有以前成功地通過

location.href = "javascript: /* some messaging code */"; 

來解決這個問題通過注入代碼也許不是最好的做法,但它的工作對我來說(也有可能是新的API,可以讓你沒有這個事。)

+0

你可以請擴展你的方法嗎?我無法理解你是如何得到數據並通過它的。我會在'「背景」感謝 – senty

0

就像我在注射從內容腳本功能或代碼的網站頁面後,上面的評論說,你可以從外部該頁面發送郵件給background.js擴展的

chrome.runtime.sendMessage("extensionIdHere", {varName: varValue}); 

在這之後,你可以有這樣的代碼來聽這些外部信息,並作出迴應或使用該被罰的價值。

chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse){ 

    if(message.varName !== undefined){ 
     //process the info 
    } 

}); 

編輯:先試試這個,如果它不工作更多下面:

manifest.json底部的添加並再次嘗試:

"externally_connectable": { 
    "matches": [ "https://theSiteWhereYouAreInjectinTheCode.com/*" ] 
} 

如果仍然沒之後嘗試將此功能添加到您的content script即可將該代碼注入網頁。

//Injects functions directly into the dom 
function exec(fn) { 
    var script = document.createElement('script'); 
    script.setAttribute("type", "application/javascript"); 
    script.textContent = '(' + fn + ')();'; 
    document.body.appendChild(script); //run the script 
    document.body.removeChild(script); //clean up 
} 

然後調用它有像這樣:

exec(function(){ 
    chrome.runtime.sendMessage("njfhnpgecmbmbipcjpkkglbkpcbjammb", {varName: "helloWorld"}); 
}); 
+0

@senty在您的清單',你有'「老大難」'設置爲TRUE;? – PredatorIWD

+0

我剛添加它。它仍然沒有返回任何東西。我無法在'background.js'中看到它。在注入腳本的sendMessage方法,我試過塞汀了回調,它確實正在發送的消息,但正如我所說 – senty

+0

@senty你可以更新在確切的代碼後我無法得到它在background.js 'background.js','manifest.json'和你的網頁,其中包括髮送外部郵件內注入的代碼? – PredatorIWD