0

I'm building a chrome extension and i got pop.js and content.js files. So far so good. But, i need to check what's the angular version of the current angular website where the extension is running. When i check the version of a specific website in console using angular.version.full command, e.g in the following website: https://angularjs.org/ , i get this version: "1.8.0". But when i try to check it through my content.js file i get this error:

Error in event handler: ReferenceError: angular is not defined at chrome-extension://mmdekcdcnkjoemkakjkbcagmgjjbdddk/content.js:19:37

Here's part of my code:

        var framework = "";
        var version = "";
        if (document.querySelector("meta[content*='Wix.com']")) {
            framework = "wix";
        } else if (!!window.React ||
            !!document.querySelector('[data-reactroot], [data-reactid]')) {
            framework = "react";
        } else if (!!window.angular ||
            !!document.querySelector('.ng-binding, [ng-app], [data-ng-app], [ng-controller], [data-ng-controller], [ng-repeat], [data-ng-repeat], [ng-version]') ||
            !!document.querySelector('script[src*="angular.js"], script[src*="angular.min.js"]')) {
            framework = "angular";
            if (document.querySelector("[ng-version]")) {
                version = document.querySelector("[ng-version]").getAttribute("ng-version");
            } else if (!!angular.version) {         //  fails here
                console.log(angular.version.full)
            } else {
                console.log("Angular version undefiend");
            }
        } else {
            framework = "other";
        }

And I am able to access and change elements in the DOM so it's not that I misplaced it or it's not loading or something.

Does anyone have any idea why it throws an error? Thanks to all the repliers :)

  • "And I am able to do other stuff on the page through with my content.js file". Can you define 'other stuff'? Just need clarification if that includes any other angularjs components/plugins or if you're just referring to basic dom access and the like. – Mark Clark Nov 17 '20 at 17:02
  • @MarkClark I'm referring to basic dom access.. I put some of my code in the description, everything is executing well except for angular websites – Anna Dmytriv Nov 17 '20 at 17:25
  • Maybe this is related? https://stackoverflow.com/a/56065203/4096074 – Mark Clark Nov 17 '20 at 17:44
  • @MarkClark I don't think so.. I'm not having trouble getting attributes from elements in the page. Just this specific command fails for some reason – Anna Dmytriv Nov 17 '20 at 22:47
  • 1
    @MarkClark Turns out you were right! I read a little more and apparently you can't access window variables from the chrome extension and the only way is to inject a script tag and run the command. Thanks so much! – Anna Dmytriv Nov 17 '20 at 23:40

0 Answers0