1

I'm trying to create a tool that monitors the usage of some JavaScript APIs in web pages. My plan is to override the JavaScript API's, store the call information into a global variable, and then send it to content script via postMessage. However, injecting code something like below into the web page causes an "Aw Snap" error.

chrome.devtools.panels.create("Test Dev Panel",
  "icon.png",
  "panel.html",
  function (panel) {
    var code = [ 
        "window.counter = 0;",
        "Array.prototype.push = function () {",
          "window.counter++;",
        "}",
        "window.addEventListener('message', function(event) {",
          "window.postMessage('Array.push() has been called ' + window.counter + ' times.', '*');",
        "}, false);"
      ];

    chrome.devtools.inspectedWindow.reload({
      injectedScript: code.join('')
    });
...
  });

It might be a security restriction. But is there any way to achieve my goal?

--- Update --- The error doesn't occur if I remove the postMessage() call. Also, it doesn't if I remove the Array.prototype overriding code. It seems to me that calling the postMessage() and overwriting Array's prototype at the same time are causing the error.

4
  • This isn't relevant any more, but you need a ; before the window.addEventListener line. "};" rather than "}"
    – Jono
    Mar 16, 2014 at 15:43
  • 1
    Aha! It was just a syntax error...
    – kuu
    Mar 17, 2014 at 16:02
  • Anyway, thank you for pointing this out Jono!
    – kuu
    Mar 17, 2014 at 16:09
  • Don't suppose you could help with my issue? stackoverflow.com/questions/22440850/…
    – Jono
    Mar 17, 2014 at 16:50

2 Answers 2

1

It's a syntax error after the Array.prototype.push.

You need to end the line with a semicolon.

...
var code = [ 
        "window.counter = 0;",
        "Array.prototype.push = function () {",
          "window.counter++;",
        "};", //<-- Here
        "window.addEventListener('message', function(event) {",
          "window.postMessage('Array.push() has been called ' + window.counter + ' times.', '*');",
        "}, false);"
      ];
...
0

The problem is with

"window.counter = 0;",
"Array.prototype.push = function () {",
"window.counter++;",
"}",
"window.addEventListener('message', function(event) {",
"window.postMessage('Array.push() has been called ' + window.counter + ' times.', '*');",
"}, false);"

code, i am not sure what you want to achieve!!

I hope you are aware of the fact Counter is always zero in a frame unless closes the window!

1
  • The API reference states that "If specified, the script will be injected into every frame of the inspected page immediately upon load, before any of the frame's scripts." developer.chrome.com/extensions/… It actually works as stated when I remove the postMessage call.
    – kuu
    Mar 5, 2013 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.