4

Given that Angular is tied to the views and bootstrapped in the main extension view, I assume the simple answer is "No, not possible", but wanted to confirm as I can't find a definitive answer anywhere.

My use case is that the extension will be polling for updated content from an API, and updating the extension's badge when found; I'd love to be able to re-use my API service from the extension's Angular codebase.

If not possible, any suggested workarounds for sharing code between the Angular extension and the background script?

4
  • I think you need to use the Event system. Commented Oct 24, 2014 at 18:52
  • Any examples of using Events in the background script to run Angular code when the popup isn't open? Commented Oct 24, 2014 at 20:15
  • 2
    You've put a bounty on a question, but as is it's not a very good question. It's somewhat too broad. Can you elaborate more on your problem? Construct a minimal example of a problem, so that an answer would implement that minimal example. Commented Oct 27, 2014 at 10:24
  • 3
    Given that Angular is tied to the views and bootstrapped in the main extension view this is not true. angularjs can be tied and boostraped from any element. angularjs will parse html only with in the bootstrapped element. Commented Oct 28, 2014 at 3:21

1 Answer 1

9

Thanks for the input @Xan, @harish - based on your feedback did some more investigation and have the solution. In essence, instead of pointing to a background script in the Chrome manifest, I am pointing to a background HTML page, which I then use to bootstrap my Angular app. The relevent code:

manifest.json:

...
"background": {
    "page": "/app/background.html"
}
...

background.html

<!DOCTYPE html>
<html lang="en" ng-app="MyApp" ng-csp>
    <head>
        <title>Background Page</title>
        <meta charset="UTF-8">
        <script type="text/javascript"
            src="/app/components/angular/angular.js"></script>
        <script type="text/javascript"
            src="/app/scripts/background.js"></script>
    </head>
    <body></body>
</html>

background.js

var app = angular.module('MyApp', []);

app.run(function() {
  console.log('Hello world');
});
Sign up to request clarification or add additional context in comments.

4 Comments

Though I'm looking for a solution for chrome app, thanks for this answer. It is really useful.
ouch.. no luck. I tried it in my current chrome app project but it doesnt worked. There is an error sayong something like this " Resource interpreted as Script but transferred with MIME type text/html: "chrome-extension://pdknlhegnpbgmbejpgjodmigodolofoi/views/background.html"."
@boi_echos - what does your manifest file look like? Can you post a new question?
I've already posted a new question yesterday. And also, I already answered my question. It seems that the "page" property in "background" is not supported in chrome packaged app, I guess. By the way here is the link to my answer - stackoverflow.com/questions/28984640/…

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.