Chrome extension: load and execute external script (tabs.executeScript)

14,881 views
Skip to first unread message

Evgeniya Ushakova

unread,
Jan 20, 2015, 1:00:52 AM1/20/15
to chromium-...@chromium.org

I have trouble loading and executing external js-script into my chrome extension.

The idea is that I want to have in my content script some default function which should parse a web-page content. And for some specific web-pages I want to load and use specific parsers, so I try to load proper js-script for a wep-page, and this script shoud extend functionality of default parser.

By now I try only execute code from external script, but have such error: Unchecked runtime.lastError while running tabs.executeScript: No source code or file specified at Object.callback

This is my manifest.json:

{
"name": "Extension name",
"version": "1.2",
"description": "My chrome extension",
"browser_action": {
    "default_popup": "popup.html",
},
"content_scripts": [{
    "css": [
        "style.css"
    ],
    "js": [
        "bower_components/jquery/dist/jquery.js",
        "bower_components/bootstrap/dist/js/bootstrap.js",
        "content.js"
    ],
    "matches": ["*://*/*"]
}],
"web_accessible_resources": [
    "frame.html",
    "logo-48.png"
],
"icons": {
    "16": "logo-16.png",
    "48": "logo-48.png",
    "128": "logo-128.png"
},
"permissions": [
    "tabs",
    "storage",
    "http://*/",
    "https://*/"
],
"manifest_version": 2

This is popup.html

<!doctype html>
 <html>
 <head>
  <title>Title</title>
  <script src="popup.js"></script>
 </head>
 <body>
  <ul>
    <li>Some link</li>
  </ul>
 </body>
</html>

And in popup.js i execute scrip like this:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.executeScript(tabs[0].id, {file: "http://127.0.0.1:8000/static/plugin/somesite.js"});
});

What am I dong wrong, did I miss something? Or should I use another approach to solve the issue?


I tried also in manifest.js:

{
"permissions": [
        "tabs",
        "activeTab",
        "storage",
        "http://127.0.0.1/*"
    ],
    "content_security_policy": "script-src 'self' http://127.0.0.1:8000; object-src 'self'",
},

but it didn't help

Tom Coleman

unread,
Jan 20, 2015, 1:53:19 AM1/20/15
to Evgeniya Ushakova, chromium-...@chromium.org
Running scripts from external sources may cause your extension to be unpublished or blocked.  If possible place all scripts within the extension. One hacky way that I do is to get the source via xml http request in a background script, and provide it to the content script by sending messages. Then eval the content to run it.
--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/22906cc4-275e-4508-930a-3d866e11707c%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Evgeniya Ushakova

unread,
Jan 20, 2015, 3:05:32 AM1/20/15
to chromium-...@chromium.org, jenu...@gmail.com
Thank you, I probably will do the same trick

вторник, 20 января 2015 г., 13:53:19 UTC+7 пользователь Tom Coleman написал:

Ammad Akhtar

unread,
Dec 9, 2015, 2:52:40 PM12/9/15
to Chromium-Extensions-Announce, jenu...@gmail.com
hi evgeniya 
i am having the same trouble and couldnt load an external js into my extension can u provide me the code for content and background page please i cant make one ....if its possible kindly tell me please . i shall be really greatful for this

Ammad Akhtar

unread,
Dec 9, 2015, 4:28:05 PM12/9/15
to Chromium-Extensions-Announce, jenu...@gmail.com
hey tom can u give an example code for this method to get the source via xml http request in a background script, and provide it to the content script by sending messages. Then eval the content to run it. along with the manifest file to me please i am unable to make a code for that @tom coleman thanks in advance :)
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Reply all
Reply to author
Forward
0 new messages