2

Currently creating a chrome extension which sits inside other applications. Currently, the application is an ember based app and therefore has it's url changes via javascript.

At some point this was working but I cannot get it to work any longer. Why doesn't it work?

        $rootScope.$watch(function(){
            return window.location;
        }, function(value){
            console.log(value)
            $rootScope.locationChanged();
        }, true)
Mohamed El Mahallawy
  • 13,024
  • 13
  • 52
  • 84

4 Answers4

0

Try using $window instead:

$scope.location = $window.location;
$watch('location', function () {
    ... // location changed
})
Peter Ashwell
  • 4,292
  • 2
  • 18
  • 22
0

location is an object, so if you are going to watch it, you need to do:

$scope.$watch('window.location', function(){
}, true);

The third parameter does a deep inspection of the properties of location, rather than the reference to location itself. This will cause the watch to fire if any of the properties change.

As a workaround you can try listening for $locationChangeSuccess events on $rootScope instead

$rootScope.$on('$locationChangeSuccess', function() {
    $rootScope.locationChanged();
}
Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75
0

What you're doing can't work. You've got access to the wrong window object:

You can't. The extension's background page runs in one process, while the tab that was updated runs in a separate process. Different processes can't share objects, so you can't directly access the window object of a tab from an extension's background page

From: https://stackoverflow.com/a/22599856/2019281

The window object the content script sees is not the same window object that the page sees.

From: https://stackoverflow.com/a/12396221/2019281

Community
  • 1
  • 1
iH8
  • 27,722
  • 4
  • 67
  • 76
  • I am currently running as content scripts and therefore run within the page and inject into the page – Mohamed El Mahallawy Feb 13 '15 at 02:26
  • You're very close though! Background does only run once :) – Mohamed El Mahallawy Feb 13 '15 at 02:27
  • What the last quote says, the window object of your content script doesn't have access to the window object of the page you're running. So a watch on window.location in your content script, watches the window object of your contentscript page, not of the page where the location is changing thus watch won't fire. – iH8 Feb 13 '15 at 02:33
  • Here's why you can inject to the page: "Although the execution environments of content scripts and the pages that host them are isolated from each other, they share access to the page's DOM." Window belongs to (or is?) the execution environment, thus isolated from the contentscript. From: https://developer.chrome.com/extensions/content_scripts#host-page-communication – iH8 Feb 13 '15 at 02:43
0

You can use the following three functions to keep track of route change

HTML

<body ng-app="myApp" ng-controller="AppCtrl">......</body>

JS

.controller('AppCtrl', function ($scope, $rootScope, $log, $location) {

  $scope.$on('$routeChangeStart', function (event, toState, toParams, fromState, fromParams) {

  });

  $scope.$on('$routeChangeSuccess', function (event, toState, toParams, fromState, fromParams) {

  });

  $scope.$on('$routeChangeError', function (event, toState, toParams, fromState, fromParams) {

  });

});
Rajeshwar
  • 2,290
  • 4
  • 31
  • 41