1

I am trying to access an Angular function from an existing website with a content script or something similar. I know the function I need to call, but I'm unfamiliar with the scope of Angular functions. So, even though I can see the function, and I know what parameters to pass into it, I can't call it from the console.

Here it the function I need...

angular.module("smartwayTraffic").controller("mapController", ["$scope", "$window", "$q", "$timeout", "$modal", "_", "constant", "geolocation", "dataService", "mapService", function(n, t, i, r, u, f, e, o, s, h) {

...

n.gotoRegion = function(t) {
    e.regionBounds.forEach(function(i) {
        i.region === t && (y(), n.map.control.fitBounds(i.bounds), v())
    })
};

....

What else do I need to trigger this function from the console?

....gotoRegion("Nashville");
tantangula
  • 314
  • 2
  • 13
  • What do you want to do exactly? This sounds like a case of [the XY Problem](http://meta.tex.stackexchange.com/questions/2449/what-does-xy-problem-mean). What is a content script exactly? What is it exactly that limits you to call gotoRegion from "a content script"? – Patrick Jan 20 '15 at 14:31
  • You might find this question useful (http://stackoverflow.com/questions/15527832/how-can-i-test-an-an-angularjs-service-from-the-console) – Matthew Abbott Jan 20 '15 at 14:32
  • The problem is that I have to take a screenshot of this website... https://smartway.tn.gov/traffic/ for every location in the menu once every 5 minutes. All I have access to is the url and a browser. The previous version of the software I am updating used cookies to zoom into these locations, but as far as I can tell, the new site only wants a geolocation point or it sends you to the wide view. The site menu that will zoom into these areas is calling a function called "gotoRegion()" with the city name as a parameter when you click the link. I am open to other solutions to this problem. – tantangula Jan 20 '15 at 14:47
  • As far what I meant by a content script, I am looking to add some kind of code that executes in the browser that manipulates the data that has been loaded. Then, I can add it into the existing script that takes screenshots of this site we already have in place. – tantangula Jan 20 '15 at 14:53
  • @tantangula: I'm not sure, but it sounds to me like what you need is a directive for the map. With a directive you are able to manipulate the DOM in whatever way you like, and can avoid writing script methods that should try to force its way into an angular controller scope. – Patrick Jan 20 '15 at 16:58
  • @Patrick I'll look into these directives. I figured out that I can call the function from the console, and it does what I need it to do. So now, the next part of the solution is to figure out how to fire the function automatically. The directives sound they might help me do this. Thanks. – tantangula Jan 20 '15 at 17:22

1 Answers1

1

Yes, you just need to use angular.element to get an element that's within the scope of your controller:

angular.element("yourElement").scope().n.gotoRegion();
Ch K V S Kumar
  • 475
  • 4
  • 13
  • This did work, but I had to traverse the scope hierarchy a little before I got to the function I needed to call. So it ultimately looks like... `angular.element(".ng-scope").scope().$$childHead.gotoRegion("Memphis")` Thanks! – tantangula Jan 20 '15 at 17:24