2012-12-04 20 views
20

我嘗試了許多方法(所有文檔化的過程),通過檢查onUpdated.addListener上的URL將腳本注入特定頁面。最後下面的代碼與'執行'似乎工作,但不完美。我可以獲取警報,但無法通過getElementById/getElementsByName找到頁面的文檔元素。必須在web_accessible_resources清單中列出資源,以便通過擴展之外的頁面加載

當我檢查頁面時,腳本被注入。但在錯誤控制檯中,我得到:

拒絕加載chrome-extension://jfeiadiicafjpmaefageabnpamkapdhe/js/Leoscript.js。必須在web_accessible_resources清單鍵中列出資源,才能通過擴展名外的頁面加載資源。

manifest.json的:

{ 
    "name": "Leo Extension for Job Boards", 
    "version": "1.6", 
    "manifest_version": 2, 
    "content_security_policy": "script-src 'self'; object-src 'self'", 
    "description": "Leo Extension", 
    "background": { 
    "scripts": ["js/Leojshelper.js"], 
    "persistent": true 
    }, 
    "content_scripts": [ 
    { 
     "matches": ["<all_urls>"], 
     "js": ["js/eventPage.js"], 
     "run_at" : "document_start" 
    } 
    ], 
    "icons":{"48":"images/bob48.png", "128":"images/bob128.png"}, //Define any icon sizes and the files that you want to use with them. 48/128 etc. 
    "browser_action": { 
    "default_icon": "images/bob.png",  // What icon do you want to display on the chrome toolbar 
    "default_popup": "LeoExtwatch.html"  // The page to popup when button clicked. 
    }, 
    "permissions": [ 
    "tabs", "<all_urls>"  // "http://*/*","https://*/*"    // Cross Site Access Requests 
    ], 
    "web_accessible_resources": ["js/LeoScript.js"] 
} 

我也給 'web_accessible_resources' 允許腳本,但仍然沒有成功。後臺腳本中的代碼:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { 
    if (changeInfo.status == 'complete') { 
     if (tab.url.indexOf("in.yahoo") !== -1) { 
      chrome.tabs.update(tabId, { url: "https://login.yahoo.com/config/mail?.intl=us" }); 
      chrome.tabs.executeScript(tabId, { 
       code: "document.body.appendChild(document.createElement('script')).src='" + 
    chrome.extension.getURL("js/LeoScript.js") + "';" 
      }, null); 

LeoScript.js中的代碼,將被注入特定的頁面。

$(document).ready(function() { 
    alert('injected'); 
    document.getElementById('username').value='aaaaaaa'; 
}); 

內容腳本:我用來注入腳本的eventPage.js。

var script = document.createElement('script'); 
    script.src = chrome.extension.getURL("js/Leoscript.js"); 
    (document.body || document.head || document.documentElement).appendChild(script); 

請指點我上面的代碼,將解決權限問題的任何變化。提前致謝。

回答

12

更新:終於想出了你的問題。在eventPage.js中,您試圖注入未列入白名單的js/Leoscript.js,而不是列入白名單的js/LeoScript.js(大寫字母爲'S')。請注意,URL是區分大小寫

chrome.tabs.executeScript(tabId, {file: 'js/LeoScript.js'}); 

LeoScript.js:

alert('injected'); 
document.getElementById('username').value='aaaaaaa'; 
+1

@方覺:謝謝你的迴應,現在累了,但同樣的結果'拒絕加載LeoScirpt:資源必須列在web_accessible_resources –

+1

我試過了再次,事情似乎更奇怪。當chrome.tabs.executeScript(tabId,{file:...})失敗時(即使代碼完全相同),chrome.tabs.executeScript(tabId,{code:...})成功。 –

+1

再次更新。請看一下。 @VineelGogineni –

11

編輯:

這是工作的版本在使用的web_accessible_resourcesInjection組合

的manifest.json

{ 
"name":"Off Screen Tabs Demo", 
"description":"This demonstrates Off Screen Tabs API", 
"manifest_version":2, 
"version":"1", 
"permissions":["tabs","<all_urls>"], 
"browser_action":{ 
    "default_icon":"screen.png", 
    "default_popup":"popup.html" 
}, 
"web_accessible_resources": ["js/LeoScript.js"] , 
"permissions":["tabs","<all_urls>"] 
} 

LeoScript.js

alert("Injected.."); 

popup.html

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

彈出。JS *

document.addEventListener("DOMContentLoaded",function(){ 
    chrome.tabs.executeScript({"file": "js/LeoScript.js"}); 
}); 

讓我知道如果你仍然有在得到它運行

問題
+0

感謝Sudarshan,當我嘗試你的solutuion時,我得到了_Error在tabs.executeScript:無法加載文件:「chrome-extension://jfeiadiicafjpmaefageabnpamkapdhe/js/LeoScript.js」._可能是我在做一些事情注入腳本時出錯,因爲我正在使用內容腳本:eventPage.js注入代碼。 –

+0

@VineelGogineni:你可以消除'js/LeoScript.js'的文件夾結構並將它保存在根路徑中。使用'{file:chrome.extension.getURL(「LeoScript.js」)}''將文件移動到根路徑後,檢查我編輯過的答案 – Sudarshan

+0

我試圖消除文件夾結構,但是同樣的錯誤出現,如果我使用_file: chrome.extension.getURL(「LeoScript.js」)_我沒有得到警覺。 –

5

許多人會登陸了這個網頁,這個錯誤的,因爲他們沒有包括他們在清單圖片/網絡資源.json文件。鏈接到api文檔是有幫助的,所以分享它:web resource in manifest

相關問題