问题描述
我已经尝试了许多方法(所有文档化的过程),通过在onUpdated.addListener上检查URL来将脚本注入特定页面。最后下面的代码与'执行'似乎工作,但不完美。我可以获取警报,但无法通过getElementById / getElementsByName找到页面的文档元素。
当我检查页面时,脚本被注入。但是在错误的控制台中,我得到:
Manifest.json:
{
name:Leo Extension for Job Boards,
版本: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
}
],
图标:{48:images / bob48.png,128:images / bob128.png} ,//定义任何图标大小以及您想要使用的文件。 48/128等
browser_action:{
default_icon:images / bob.png,//你想在chrome工具栏上显示什么图标
default_popup :LeoExtwatch.html//点击按钮时弹出的页面。
},
权限:[
tabs,< all_urls> //http:// * / *,https:// * / *//跨站点访问请求
],
web_accessible_resources:[js / LeoScript.js]
}
我也给脚本授予了'web_accessible_resources'权限,但仍然没有成功。代码在后台脚本中:
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo, {
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中的代码将被注入特定页面。 p>
$(document).ready(function(){
alert('injected') ;
document.getElementById('username')。value ='aaaaaaa';
});
内容脚本:我用来注入的eventPage.js ct script。
$ b
var script = document.createElement('script');
script.src = chrome.extension.getURL(js / Leoscript.js);
(document.body || document.head || document.documentElement).appendChild(script);
请指出上述代码中的任何更改,以解决权限问题。提前致谢。
更新:终于想出了您的问题。在eventPage.js中,您试图注入未列入白名单的js / Leoscript.js,而不是列入白名单的js / LeoScript.js(大写字母为'S')。请注意,URL是区分大小写的!
chrome.tabs.executeScript(tabId,{file: 'JS / LeoScript.js'}); b
$ b LeoScript.js: > 警报( '注入');
document.getElementById('username')。value ='aaaaaaa';
I have tried many ways( all documented procedures)to inject script into a specific page upon checking URL at onUpdated.addListener. Finally the below code with 'executescript' seems to work, but not perfectly. I could able to get alerts but can not able to find document elements of the page through getElementById/getElementsByName.
When I inspected the page, script is injected. But in error console I get:
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"]
}
I have also given 'web_accessible_resources' permission to the script, but still no success. Code in background script:
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);
Code in LeoScript.js, which will be injected into specific page.
$(document).ready(function () {
alert('injected');
document.getElementById('username').value='aaaaaaa';
});
Content Script :eventPage.js which I used to inject script.
var script = document.createElement('script');
script.src = chrome.extension.getURL("js/Leoscript.js");
(document.body || document.head || document.documentElement).appendChild(script);
Please point me at any changes in the above code that will solve the permission issues. Thanks in advance.
UPDATE: Finally figured out your problem. In eventPage.js, you tried to inject js/Leoscript.js, which is NOT whitelisted, instead of js/LeoScript.js (with a capital 'S'), which is whitelisted. Note that URLs are case-sensitive!
chrome.tabs.executeScript(tabId, {file: 'js/LeoScript.js'});
LeoScript.js:
alert('injected');
document.getElementById('username').value='aaaaaaa';
这篇关于必须在web_accessible_resources清单键中列出资源,才能通过扩展名外的页面加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!