Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

google chrome - Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension

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:

Denying load of chrome-extension://jfeiadiicafjpmaefageabnpamkapdhe/js/Leoscript.js. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EDIT:

This is working version where combination of web_accessible_resources and Injection is used

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>

popup.js*

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

Let me know if you still have problem in getting it running


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...