5

I am running into a strange issue while using angularjs template in a content script based chrome extension. I go in an infinite loop. When I use an inline template (using template attribute with a string), it works fine. Can anybody suggest what I am doing wrong?

manifest.json

{
  "name": "Content Script Cross-Domain XMLHttpRequest Example",
  "version": "2.0.0",
  "manifest_version": 2,
  "description": "Demonstrates making cross domain requests from a content script by putting Twitter trends on Google News.",
  "permissions": [
    "http://localhost:9393/*"
  ],
  "icons": {
    "48" : "sample-48.png",
    "128" : "sample-128.png"
  },
  "content_scripts": [
    {
      "matches": ["http://news.google.com/*","*://mail.google.com/*"], 
      "js" : ["lib/jquery-1.6.4.min.js","lib/angular-1.0.1.min.js", "app.js","contentscript.js"]
    }
  ]
}

app.js

angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  console.log('inside angular.module');
  $routeProvider.
      when('/', {templateUrl: 'contact.html',   controller: AppController}).
      otherwise({redirectTo: '/'});
}]);


function AppController($scope){
  console.log('inside AppController');
}

inside contentscript.js

$(this).append('<div id="gmeAppContainer">'
                                 + '<div ng-view></div>'
                              + '</div>');
var rootEle = $(this).find('#gmeAppContainer');
angular.bootstrap(rootEle,['myApp']);

When I use inline tempate in app.js,it works fine.

when('/', {template: '<div>This is inline template </div>',   controller: AppController}).

Have also posted on angularjs google group https://groups.google.com/d/topic/angular/A_SVYZWPKe8/discussion

1 Answer 1

4

It looks like I figured it out.

First declare the template file in manifest.json file.

  "web_accessible_resources" :[
      "contact.html"
  ]

Then use the chrome.extension.getURL method to get the absolute url to the template file

  when('/', { controller: AppController, templateUrl : chrome.extension.getURL('contact.html')}).
Sign up to request clarification or add additional context in comments.

1 Comment

I got the angular error message that the url is untrusted, so I added $sce.trustAsResourceUrl: ``` $sce.trustAsResourceUrl(chrome.extension.getURL('html/drive-properties.html')) ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.