2

I'm trying to include content script into tabs which is loaded from the Web (eg. https://www.somesite.com/somescript.js ). This is not a library, rather a frequently (every week) changing script. As the change is so frequent I do not want the users to keep updating so often. I have looked at the content security policy and added my domain there, which has allowed me to include background scripts from the Web but this doesn't seem to work for content scripts.

PS. I tried using some JS loaders (since I needed multiple files and a callback), but they inject the script in the isolated world of the tab and not the content script.

  • 1
    possible duplicate of [Chrome extension adding external javascript to current page's html](http://stackoverflow.com/questions/10285886/chrome-extension-adding-external-javascript-to-current-pages-html) – Rob W Jul 15 '13 at 17:33
  • Do know the security implications of running code from a third-party in a content script. If you're fine with that, skip to section ["Method 2: Injecting a Up-to-date GA" of this answer](http://stackoverflow.com/a/10371025/938089). – Rob W Jul 15 '13 at 17:35
  • 1
    @RobW Great answer! But I would want to avoid `eval` the evil villain, is there anyway to do that? Maybe use `chrome.tabs.executeScript(tabId, {code: code}` from the BG.. – user2079994 Jul 15 '13 at 18:11
  • @RobW BTW, why does the restriction only apply to content scripts and not background script. – user2079994 Jul 15 '13 at 18:12
  • 1
    Blindly avoiding `eval` is bad. In this case, you really want to "parse and run a string as JavaScript", so use of `eval` is justified. `chrome.tabs.executeScript` would serve the same task as eval, except that it's only available in the context of a background script (note that the [previous revision of my answer](http://stackoverflow.com/revisions/10371025/3) also used `chrome.tabs.executeScript` because the `chrome.storage` API did not exist back then). What restrictions are you referring to? – Rob W Jul 15 '13 at 19:09
  • @RobW I will ponder upon your argument for a justified `eval`. – user2079994 Jul 17 '13 at 18:48
  • The restriction, as I stated in the question: _I can successfully include an external content script in the background page while it doesn't work in content scripts._ Could it be bcz the background script runs in the same **world** as the bg page? – user2079994 Jul 17 '13 at 19:06
  • Background page === background script. I really wonder what you mean by "include external **content script** in bg page". A [content script](https://developer.chrome.com/extensions/content_scripts.html) cannot run in the background page, only in tabs. If you don't know how to phrase what you've tried, consider adding the code to the question. About eval: What you want is to load an external script and execute it in a content script, right? In other words, you want to get a string from somewhere else, and let the JavaScript interpreter run it. That task is well-suited for `eval`. – Rob W Jul 17 '13 at 19:56
  • People, when you answer via a comment instead of a formal answer, it leaves the question cluttering Stack Overflow's unanswered questions view. Please don't do that. – Pixievolt No. 1 Jan 23 '14 at 08:15

0 Answers0