0

I inherited the a extension that is used internally on Chromebooks to pass authentication to a web server for the currently logged in user. The extension itself works fine as is right now with manifest V2 but we are trying prepare for manifest v3 and are having issues getting the extension to fire.

Here is an example of the extension as it is currently setup.

Manifest File

{
"background": {
  "scripts": [ "background.min.js" ]
},
"description": "User Agent",
"icons": {
  "128": "icon.png",
  "16": "icon.png"
},
"manifest_version": 2,
"name": "User Agent",
"permissions": [ "background", "identity", "identity.email", "http://*/", "https://*/" ],
"version": "1.0.3"
}

Background File

(function() {
var e = void 0,
    n = new Event("userInfoChanged"),
    t = 0,
    a = function() {
        chrome.identity.getProfileUserInfo(function(a) {
            var o = !1;
            o = (new Date).getTime() > t + 179e4 || e != a.email, e = a.email, o && (n.username = e, document.dispatchEvent(n))
        })
    };
document.addEventListener("userInfoChanged", function(e) {
    t = (new Date).getTime();
    var n = new XMLHttpRequest;
    n.open("GET", "URL_TO_WEB_SERVER" + encodeURIComponent(btoa(e.username)), !0), n.send()
}, !1), setInterval(a, 6e4), a(), setInterval(function() {
    n.username = e, document.dispatchEvent(n)
}, 18e5)
 }).call(this);

From what I have read to update this you need to move from "background page" to using a "service worker"

We need to the authentication that is happening in the current background.min.js file to happen on boot / extension install. I have been reading and it looks as though using the chrome.alarms is the way to go.

I have updated the manifest file

Updated Manifest File

{

 "background": {
  "service_worker": "background.min.js"
 },
 "description": "User Agent",
 "icons": {
    "128": "icon.png",
    "16": "icon.png"
 },
 "manifest_version": 3,
 "name": "User Agent",
 "permissions": ["alarms", "background", "identity", "identity.email", "http://*/", 
"https://*/" ],
"version": "1.2.0"
}

and updated the background.min.js file

Updated Background File

chrome.alarms.onAlarm.addListener(a => {
console.log('Alarm! Alarm!', a);
});

 chrome.runtime.onInstalled.addListener(() => {
 chrome.alarms.get('alarm', a => {
 if (!a) {
   chrome.alarms.create('alarm', {periodInMinutes: 1});
 }
});
  });



  (function() {
  var e = void 0,
     n = new Event("userInfoChanged"),
     t = 0,
     a = function() {
         chrome.identity.getProfileUserInfo(function(a) {
            var o = !1;
            o = (new Date).getTime() > t + 179e4 || e != a.email, e = a.email, o && (n.username = e, document.dispatchEvent(n))
        })
    };
document.addEventListener("userInfoChanged", function(e) {
    t = (new Date).getTime();
    var n = new XMLHttpRequest;
    n.open("GET", "URL_TO_WEB_SERVER" + encodeURIComponent(btoa(e.username)), !0), n.send()
}, !1), setInterval(a, 6e4), a(), setInterval(function() {
    n.username = e, document.dispatchEvent(n)
}, 18e5)
}).call(this);

I am able to get the extension to install but it appears that the function in the background script is not running. I am a more of a systems guy and am more comfortable with PowerShell. This fell in my lap but I am very interested in figuring this out. Any help would be appreciated.

RocRaider
  • 21
  • 2
  • If there's a lot of code you'll have to hire someone to rewrite it properly. If that's all you should be able to do it. Since there's no `document` in a service worker you'll need to make a normal function named userInfoChanged and call it directly. The biggest issue though is that your extension will restart the service worker every minute because service worker dies in 30 seconds, so it'll drain the battery. You can use some [workarounds](/a/66618269) but the proper fix is to find a different way to detect the change in the user name. Ah, also use `fetch` instead of XMLHttpRequest. – wOxxOm Sep 30 '22 at 18:42
  • @wOxxOm this is the entirety of the code. Could you give me an example of what you mean as far as "make a normal function"? My goal would be to possibly have the extension pass authentication on install. – RocRaider Sep 30 '22 at 18:49
  • Well, just a standard function, you can see examples in any tutorial/documentation on JS. – wOxxOm Sep 30 '22 at 18:52
  • Can you get the source code for the MV2 extension? (not minified, and with comments) – Thomas Mueller Sep 30 '22 at 19:36
  • @ThomasMueller the source code for the MV2 extensions is in the post. All it contains is a manifest file and the background.js file. – RocRaider Sep 30 '22 at 20:12

1 Answers1

0

I also wrote to Reddit , but it seems that it is not reflected

  1. Add alarms to permissions in manifest.json
  2. Write alarm registration function and activation process in your background.js
    const ALARM_NAME = "alarmName";
    
    // Alarm registration function
    function addTimer(): void {
        chrome.alarms.create(ALARM_NAME, {
            periodInMinutes: 15,
        });
        chrome.alarms.onAlarm.addListener((alarm) => {
            if (alarm.name === ALARM_NAME) {
                periodicFunctionYouLike();
            }
        });
    }
    
    // Register if there is no alarm with the specified name
    chrome.alarms.get(ALARM_NAME)
        .then((alarm) => {
            if (!alarm) {
                addTimer();
            }
        }).catch(() => {
            addTimer();
        });

In this process, Google Drive synchronization is actually realized on a regular basis with our chrome extension.

S LT
  • 1