Skip to content

Commit 9bce3e8

Browse files
authored
Handle back/forward navigation (#66)
When navigating using back/forward, the action can get out of sync with the page. Listen to the `pageshow` event to ensure the action state is correct. https://web.dev/articles/bfcache
1 parent fdb89fd commit 9bce3e8

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/background_scripts/background.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
downloads,
32
runtime,
43
pageAction,
54
tabs as _tabs,

src/content_scripts/on-loaded.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { runtime } from "webextension-polyfill";
22

3-
runtime
4-
.sendMessage({
5-
action: "showPageAction",
6-
})
7-
.catch((err) =>
8-
console.error("Failed to send 'showPageAction' message", err)
9-
);
3+
function showPageAction() {
4+
runtime.sendMessage({ action: "showPageAction" }).catch((err) => {
5+
console.error("Failed to send 'showPageAction' message", err);
6+
});
7+
}
8+
9+
// Detect history navigation (back/forward)
10+
window.addEventListener("pageshow", () => {
11+
showPageAction();
12+
});
13+
14+
showPageAction();

0 commit comments

Comments
 (0)