0

I want to get $scope value so I can change the data values in it through chrome extension.. I have used the following code to get the $scope

var $scope = angular.element(document.getElementById('name')).scope()

It work fine when I try it on Console.but when I put the code in extension it gave me this error . angular is not defined So I also added angular.js file in my chrome js file . But still i could not get the $scope and does not able to change the data . This is the code I am using `

$scope = angular.element(document.getElementById('name')).scope();

    console.log($scope);
    $scope.$apply(function () {
        $scope.data.contact_type = 20;
        $scope.data.contact_name = $('#Contactname').val();
        $scope.data.contact_company = $('#contactCompany').val();
        $scope.data.contact_email = $('#contactEmail').val();
        $scope.data.contact_phone = $('#contactPhone').val().replace('/', '-');
        $scope.data.hide_contact_email = 1;

    });`

Kindly anyone help me to get the $scope and change it in the extension as well .

Moeezalikhan
  • 41
  • 3
  • 10

1 Answers1

1

You need to add angular to your manifest.json

Here's is an example

{
    "manifest_version": 2,
    "name": "your extension",
    "version": "0.1",
    "content_scripts": [
        {
            "matches": [
                "<all_urls>"
            ],
            "js": ["angular.min.js","content.js"]
        }
    ]    
}
Gerard Cuadras
  • 1,655
  • 1
  • 17
  • 23
  • Sorry I have already tried it .. but it didnt work for me . here is the js file of mine `"js": ["js/jqueryui-min.js", "js/blockui.js", "lib/dataplace.js","js/angular.js"]` – Moeezalikhan Feb 02 '16 at 09:00
  • @Moeezalikhan Order matters. You load Angular after you try to use it. – Xan Feb 02 '16 at 09:14
  • @Xan is correct, try to load angular before. – Gerard Cuadras Feb 02 '16 at 09:29
  • Probably won't help anyway though; I imagine copies of Angular in different contexts will conflict. – Xan Feb 02 '16 at 09:29
  • I have change it to This but still it didnt get the Scope `"js": ["js/angular.js","js/jqueryui-min.js", "js/blockui.js", "lib/dataplace.js"]` – Moeezalikhan Feb 02 '16 at 09:30
  • This will not work, and if you want an analogy: suppose you have a program with _some_ of its data on a network share. What you're trying to do is to run a second copy of the program, in parallel, on another computer, and try to control the first one. In this analogy, computers are JS contexts, and network share is the DOM. I closed this as a duplicate of a question where 3 possible solutions are represented. – Xan Feb 02 '16 at 11:16