0

I tried to build simple extension according to the examples: from here and here
and I must be doing something wrong here.
All I want is for start that on each loading page ( or even better before loading ) it will popup hello alert

manifest.json

{
   "background": {
      "page": "background.html"
   },
   "content_scripts": [
    {
      "matches": ["http://*/*","https://*/*"],       
      "js": ["jquery-1.8.2.min.js","contentscript.js"],
      "run_at": "document_end"
    }
   ],
   "web_accessible_resources": [
    "script_inpage.js"
   ],
   "browser_action": {

      "default_popup": "popup.html",
      "default_title": "Test 123"
   },
   "content_security_policy": "script-src 'self'; media-src *; object-src 'self'",
   "description": "Simple Test 123.",

   "manifest_version": 2,
   "minimum_chrome_version": "18",
   "name": "Test 123",
   "permissions": [ 
        "unlimitedStorage",
        "http://*/",
        "tabs",
   ],

   "version": "2.6"
}

background.html is empty for now

contentscript.js:

var s = document.createElement('script');
s.src = chrome.extension.getURL('script_inpage.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.parentNode.removeChild(s);
};

script_inpage.js:

alert("this is script");

There is no error even when I trigger the developer console window.

Community
  • 1
  • 1
user63898
  • 29,839
  • 85
  • 272
  • 514

1 Answers1

1

Your manifest file is invalid, there's a superfluous comma after the last entry in the "permissions" section:

"permissions": [ 
    "unlimitedStorage",
    "http://*/",
    "tabs", // <-- Remove that comma
],

If you want your script to run as early as possible, use "run_at": "document_start" instead of "document_end". Then, your script will run before <head> and <body> even exist.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thanks for answering , when i some tab im getting : Failed to load resource when i disable the ext , there is no error ( just to be sure its the extention ) – user63898 Nov 11 '12 at 14:32
  • 1
    @user63898 I have no idea what you're talking about. Your problem description is vague, and your manifest file is cluttered with unused features. Can you clarify your intents and elaborate the problem you're experiencing? – Rob W Nov 11 '12 at 14:37
  • i just want to be able to triger script ( in this case simple alert) on each page . the manifest is taken from sample script i found – user63898 Nov 11 '12 at 14:40
  • 1
    @user63898 Your extension works fine when I tested it locally. In addition, I have reduced the extension to the minimum, see http://pastebin.com/uCvJNdiR. If you want to run the code before page load, add a comma and `"run_at": "document_start"` at the `content_scripts` section. – Rob W Nov 11 '12 at 14:45
  • Thank you very much ,why then i need the scrip.js as in the examples – user63898 Nov 11 '12 at 15:14
  • @user63898 Because, in these examples, the developers need to access global variables in the **context of the page**. For an explanation, I refer to http://stackoverflow.com/a/9916089/938089?chrome-extension-code-vs-content-scripts-vs-injected-scripts. – Rob W Nov 11 '12 at 15:18
  • Just to update you , but my initial script working fine , what i had to do is to close the browser and reopen it again . i was thinking that open new tab is enough .. i guess not – user63898 Nov 12 '12 at 08:15
  • 1
    @user63898 "Initial script"? Not the one you posted in the question, I guess, because the manifest file is invalid. For development, I recommend to use [Chrome Extension Reloader](https://github.com/Rob--W/Chrome-Extension-Reloader) to reload the extension (including cache). – Rob W Nov 12 '12 at 11:53