Skip to content

Commit

Permalink
fix: attempts at fixing init and consent (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed May 22, 2023
1 parent b53d099 commit a363a10
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 64 deletions.
1 change: 0 additions & 1 deletion manifest.json
Expand Up @@ -22,7 +22,6 @@

"background": {
"scripts": [
"static/installed.js",
"out/app.js"
],
"persistent": false
Expand Down
112 changes: 67 additions & 45 deletions src/eventPage.js
@@ -1,28 +1,27 @@
"use strict";
/*
* This code uses event pages, a special webextension thingy documented here:
* https://developer.chrome.com/extensions/event_pages
*/

var client = require("./client.js")
var client = require("./client.js");

"use strict";

// Mininum guaranteed in chrome is 1min
var check_interval = 5;
var max_check_interval = 60;
var heartbeat_interval = 20;
var heartbeat_pulsetime = heartbeat_interval + max_check_interval;


function getCurrentTabs(callback) {
// Query filter to be passed to chrome.tabs.query - see
// https://developer.chrome.com/extensions/tabs#method-query
var queryInfo = {
active: true,
currentWindow: true
currentWindow: true,
};

chrome.tabs.query(queryInfo, function(tabs) {
chrome.tabs.query(queryInfo, function (tabs) {
// TODO: Won't necessarily work when code is run as a background plugin instead of as a popup
// chrome.tabs.query invokes the callback with a list of tabs that match the
// query. When the popup is opened, there is certainly a window and at least
Expand All @@ -39,24 +38,36 @@ var last_heartbeat_time = null;
function heartbeat(tab, tabCount) {
//console.log(JSON.stringify(tab));
var now = new Date();
var data = {"url": tab.url, "title": tab.title, "audible": tab.audible, "incognito": tab.incognito, "tabCount": tabCount};
var data = {
url: tab.url,
title: tab.title,
audible: tab.audible,
incognito: tab.incognito,
tabCount: tabCount,
};
// First heartbeat on startup
if (last_heartbeat_time === null){
if (last_heartbeat_time === null) {
//console.log("aw-watcher-web: First");
client.sendHeartbeat(now, data, heartbeat_pulsetime);
last_heartbeat_data = data;
last_heartbeat_time = now;
}
// Any tab data has changed, finish previous event and insert new event
else if (JSON.stringify(last_heartbeat_data) != JSON.stringify(data)){
else if (JSON.stringify(last_heartbeat_data) != JSON.stringify(data)) {
//console.log("aw-watcher-web: Change");
client.sendHeartbeat(new Date(now-1), last_heartbeat_data, heartbeat_pulsetime);
client.sendHeartbeat(
new Date(now - 1),
last_heartbeat_data,
heartbeat_pulsetime
);
client.sendHeartbeat(now, data, heartbeat_pulsetime);
last_heartbeat_data = data;
last_heartbeat_time = now;
}
// If heartbeat interval has been exceeded
else if (new Date(last_heartbeat_time.getTime()+(heartbeat_interval*1000)) < now){
else if (
new Date(last_heartbeat_time.getTime() + heartbeat_interval * 1000) < now
) {
//console.log("aw-watcher-web: Update");
client.sendHeartbeat(now, data, heartbeat_pulsetime);
last_heartbeat_time = now;
Expand All @@ -68,16 +79,16 @@ function heartbeat(tab, tabCount) {
*/

function createNextAlarm() {
var when = Date.now() + (check_interval*1000);
chrome.alarms.create("heartbeat", {"when": when});
var when = Date.now() + check_interval * 1000;
chrome.alarms.create("heartbeat", { when: when });
}

function alarmListener(alarm) {
if(alarm.name === "heartbeat") {
getCurrentTabs(function(tabs) {
if(tabs.length >= 1) {
chrome.tabs.query({}, function(foundTabs) {
heartbeat(tabs[0], foundTabs.length);
if (alarm.name === "heartbeat") {
getCurrentTabs(function (tabs) {
if (tabs.length >= 1) {
chrome.tabs.query({}, function (foundTabs) {
heartbeat(tabs[0], foundTabs.length);
});
} else {
//console.log("tabs had length < 0");
Expand All @@ -101,8 +112,8 @@ function stopAlarmListener() {
*/

function tabChangedListener(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function(tab) {
chrome.tabs.query({}, function(foundTabs) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
chrome.tabs.query({}, function (foundTabs) {
heartbeat(tab, foundTabs.length);
});
});
Expand Down Expand Up @@ -139,7 +150,7 @@ function stopWatcher() {

function popupRequestReceived(msg) {
if (msg.enabled != undefined) {
chrome.storage.local.set({"enabled": msg.enabled});
chrome.storage.local.set({ enabled: msg.enabled });
if (msg.enabled) {
startWatcher();
} else {
Expand All @@ -152,15 +163,14 @@ async function askConsentNeeded() {
// Source for compatibility check: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Build_a_cross_browser_extension#handling_api_differences
try {
if (typeof browser.runtime.getBrowserInfo != "function") {
return false
return false;
}
} catch (e) {
return false
return false;
}
let browserInfo;
await browser.runtime.getBrowserInfo().then((info) => {browserInfo = info})
let browserInfo = await browser.runtime.getBrowserInfo();
if (browserInfo.name != "Firefox") {
return false
return false;
}

// Mozilla Addons doesn't allow bypassing the consent dialog through managed storage,
Expand All @@ -179,35 +189,47 @@ async function askConsentNeeded() {
return true;
}

function startPopupListener() {
chrome.storage.local.get(["enabled"], async function(obj) {
function getConsentThenStart() {
// consentGiven is set in static/consent.js
chrome.storage.local.get("consentGiven", (obj) => {
if (obj.consentGiven === true) {
startWatcher();
} else {
const url = chrome.runtime.getURL("../static/consent.html");
chrome.windows.create({
url,
type: "popup",
height: 550,
width: 416,
});
}
});
}

/*
* Init
*/

function init() {
chrome.storage.local.get("enabled", async function(obj) {
// obj.enabled is undefined when the extension is first installed,
// or if we need user consent, and they have yet to give it.
if (obj.enabled == undefined) {
if(await askConsentNeeded()) {
chrome.storage.local.set({"enabled": false}); // TODO: replace with storage.managed
chrome.storage.local.set({"noConsentGiven": true});
chrome.storage.local.get("askConsent", (obj) => {
if(obj.askConsent) {
const url = chrome.runtime.getURL("../static/consent.html");
chrome.windows.create({ url, type: "popup", height: 550, width: 416, });
freshInstall = false;
}
})
if (await askConsentNeeded()) {
getConsentThenStart();
} else {
chrome.storage.local.set({"enabled": true});
// If we don't need consent, enable by default
chrome.storage.local.set({ enabled: true });
startWatcher();
}
} else if (obj.enabled) {
startWatcher();
}
});
// Listens to enables/disables by toggling the checkbox in the popup.
chrome.runtime.onMessage.addListener(popupRequestReceived);
}

/*
* Init
*/

(function() {
startPopupListener();
// startWatcher() moved to startPopupListener
(function () {
init();
})();
2 changes: 1 addition & 1 deletion static/consent.js
Expand Up @@ -8,8 +8,8 @@ function consentListeners() {
window.close()
});
consent_given.addEventListener("click", (obj) => {
chrome.storage.local.set({"consentGiven": true});
chrome.runtime.sendMessage({enabled: true}, function(response) {});
chrome.storage.local.set({"noConsentGiven": false});
window.close()
})
}
Expand Down
11 changes: 0 additions & 11 deletions static/installed.js

This file was deleted.

12 changes: 6 additions & 6 deletions static/popup.js
Expand Up @@ -8,14 +8,14 @@ function renderStatus() {

// Consent Button
let showConsentBtn = document.getElementById('status-consent-btn');
chrome.storage.local.get("noConsentGiven", (obj) => {
console.log('noConsentGiven: ', obj.noConsentGiven)
if (obj.noConsentGiven) {
enabledCheckbox.setAttribute('disabled', '');
showConsentBtn.style.display = 'inline-block';
} else {
chrome.storage.local.get("consentGiven", (obj) => {
console.log('consentGiven: ', obj.consentGiven)
if (obj.consentGiven) {
enabledCheckbox.removeAttribute('disabled');
showConsentBtn.style.display = 'none';
} else {
enabledCheckbox.setAttribute('disabled', '');
showConsentBtn.style.display = 'inline-block';
}
});

Expand Down

0 comments on commit a363a10

Please sign in to comment.