Skip to content

Commit

Permalink
Revert "Remove alarm that worked around a Chromium bug"
Browse files Browse the repository at this point in the history
This reverts commit 5152798.
  • Loading branch information
ssorallen committed May 15, 2024
1 parent 5e59164 commit b113950
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ your tabs but not the content inside those tabs.
Tab Wrangler's requested permissions are listed in its [manifest.json][manifest.json] under the
`"permissions"` key.

- [`"alarms"`][7]: Allows creation of alarms to periodically check Tab Wrangler's background script
that checks for stale tabs is running and healthy.
- [`"contextMenus"`][3]: Allows a "Tab Wrangler" menu item when you right click on a webpage that
lets you send the tab to the Tab Corral, lock that tab, or lock all tabs on that domain.
- [`"sessions"`][4]: Allows Tab Wrangler to read and restore the full history of a tab including
Expand Down Expand Up @@ -186,3 +188,4 @@ working and tested, submit a pull request to this primary project and we'll get
[5]: https://developer.chrome.com/extensions/storage
[6]: https://developer.chrome.com/extensions/tabs
[manifest.json]: https://github.com/tabwrangler/tabwrangler/blob/main/app/manifest.json
[7]: https://developer.chrome.com/docs/extensions/reference/alarms/
25 changes: 25 additions & 0 deletions app/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,31 @@ async function startup() {

startup();

// Keep the [service worker (Chrome) / background script (Firefox)] alive so the popup can always
// talk to it to access the Store.
// Self-contained workaround for https://crbug.com/1316588 (Apache License)
// Source: https://bugs.chromium.org/p/chromium/issues/detail?id=1316588#c99
let lastAlarm = 0;
(async function lostEventsWatchdog() {
let quietCount = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
await new Promise((resolve) => setTimeout(resolve, 65000));
const now = Date.now();
const age = now - lastAlarm;
console.debug(`lostEventsWatchdog: last alarm ${age / 1000}s ago`);
if (age < 95000) {
quietCount = 0; // alarm still works.
} else if (++quietCount >= 3) {
console.warn("lostEventsWatchdog: reloading!");
return chrome.runtime.reload();
} else {
chrome.alarms.create(`lostEventsWatchdog/${now}`, { delayInMinutes: 1 });
}
}
})();

chrome.alarms.onAlarm.addListener(() => (lastAlarm = Date.now()));
chrome.runtime.onMessage.addListener((message) => {
if (message === "reload") {
console.warn("[runtime.onMessage]: Manual reload");
Expand Down
15 changes: 3 additions & 12 deletions app/manifest.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@
"open_in_tab": true,
"page": "popup.html"
},
"permissions": [
"contextMenus",
"sessions",
"storage",
"tabs"
],
"permissions": ["alarms", "contextMenus", "sessions", "storage", "tabs"],
"web_accessible_resources": [
{
"matches": [
"*://*/*"
],
"resources": [
"img/icon48.png"
]
"matches": ["*://*/*"],
"resources": ["img/icon48.png"]
}
]
}

0 comments on commit b113950

Please sign in to comment.