0

I want to override CanvasRenderingContext2D and I'm using this code to do that:

(function() {

    var getStackTrace = function() {
        var obj = {};
        Error.captureStackTrace(obj, getStackTrace);
        return obj.stack;
    };
    var orig_fillText = CanvasRenderingContext2D.prototype.fillText;

    CanvasRenderingContext2D.prototype.fillText = function() {
        if(arguments !== undefined){
            var stackTrace = getStackTrace();
            if(stackTrace.indexOf("myText") != -1){
                console.log(arguments[0]);
            }
        }
        orig_fillText.apply(this, arguments);
    };
}());

That code is working fine if I execute it directly on the console, but is not working if I execute it from my Chrome Extension content script (I'm adding new HTML to the page with the same extension and that part is working, so the chrome extension is not the problem).

I can note my global vars are not shared with the JS from the website either so I guess is related with some type of isolation?

Enrique
  • 4,693
  • 5
  • 51
  • 71
  • 1
    Pretty closely related: https://stackoverflow.com/q/10526995/710446 (disclose: the top answer is my own). There might be a better duplicate, but that will get you started, at least. You actually *don't* want the "new" solution at the top of my answer, since you're not interested in message passing; instead, you want to read the first half of the "old" answer to understand [how execution environments work](https://developer.chrome.com/extensions/content_scripts#execution-environment). – apsillers Nov 13 '18 at 21:09
  • 1
    A better duplicate might be https://stackoverflow.com/a/12396221/710446, the end of which is boiled down to the `createElement`/`appendChild` approach you need, without the extra stuff in my above suggestion. – apsillers Nov 13 '18 at 21:22
  • Everything @apsillers wrote should be covered in the canonical dupe target I've linked. – wOxxOm Nov 14 '18 at 08:07

0 Answers0