Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS from a Chrome Extension background script

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?

like image 576
Unpossible Avatar asked Jan 10 '23 13:01

Unpossible


1 Answers

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');
});
like image 88
Unpossible Avatar answered Jan 22 '23 07:01

Unpossible