1

I have written a javascript code to print the active function calls of a webpage. When I paste the code in the console I can see the active functions in the console and the problem is with the extension. I converted the javascript code to google chrome extension and it is not working. I am new to extension creation.

Plugin Code:

manifest.json

{
  "manifest_version": 2,
  "name": "example",
  "version": "0.1",
  "description": "My Chrome Extension",
  "icons": {
  },

  "background": {
    "scripts": [
      "js/background.js"
    ]
  },

  "browser_action": {
    "default_title": "My test Environment"
  },

  "permissions": [
    "background",
    "storage",
    "tabs",
    "http://*/*",
    "https://*/*"
  ]
}

js/background.js

function tracecalls(par) {
    for (var name in window) {
        if (typeof window[name] === 'function') {

            window[name] = (function (fname, f) {
                return function () {
                    par(fname);

                    return f.apply(this, arguments);
                }
            }(name, window[name]));
        }
    }
}
tracecalls(function (fname) {
    console.log("Function Name : " + fname);
});
UserName
  • 895
  • 7
  • 17
Sunil
  • 13
  • 4

1 Answers1

0

Your background script runs in a separate page; even if the code did output something you're probably looking in the wrong console.

Take time to read through Architecture Overview. If you want to interact with an existing tab, you need a Content Script.

But even if you set up a content script with this code, you won't see anything. Why? Because content script code and page code are cleanly separated (and window object is not shared).

To interact with page code, another step, page-level injection, is required. If you do that, it should not be different from running the snippet in the console (that, by default, executes code you enter in page's context).

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • I got it.. I injected the script in every page we open in the browser and the problem solved. Thanks for the suggestions. – Sunil May 27 '16 at 23:21