Search code examples
consolechrome-dev-editor

How to use Chrome Dev Tools script in its console?


I've created a simple plugin for Chrome, that provide me some functions, for example, a function to create a new cookie when pressing some keys.

When the page loads, I can see that the file was loaded (because if I print something in this plugin, Chrome shows me where is the JS that did that.).

My question is: how can I use the function of this js in console?

Example of my plugin:

manifest.json

{
    "name":"Function helpers",
    "description":"",
    "version":"1",
    "manifest_version":2,
    "content_scripts": [{
        "matches": ["http://localhost:9999/*"],
        "js": ["jquery.js", "myscript.js"]
    }
  ]
}

myscript.js

   console.log("It works!");
    var helper = {
      createCookie =  function (){
           console.log("Cookie created!");
      }

    }

Chrome's console shows me:

It works!                       - myscript.js:1

I would like to be able to call the function helper.createCookie() of myscript.js file in Chrome's console.


Solution

  • Seems like you can't.

    Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.

    http://developer.chrome.com/extensions/content_scripts.html#execution-environment