Skip to content

Commit

Permalink
Add: tab monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Nov 18, 2024
1 parent 7e90276 commit 7add894
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import browser from "webextension-polyfill";

import {pref} from "./lib/pref.js";
import {createTab} from "./lib/tab.js";
import {tabMonitor} from "./lib/tab-monitor.js";
import {fetchImage} from "./lib/fetch-image.js";
import {download} from "./lib/downloader.js";
import {imageCache} from "./lib/image-cache.js";
Expand Down Expand Up @@ -143,7 +144,7 @@ const MENU_OPTIONS = [
handler(tab, info.frameId);
},
contexts: ["page", "image"],
oncontext: () => pref.get("contextMenu")
oncontext: () => pref.get("contextMenu") && !tabMonitor.isExtensionPage()
}))
];

Expand All @@ -166,6 +167,7 @@ if (menus) {
}
});
});
tabMonitor.on("change", () => menus.update());
}

// setup dynamic icon
Expand Down
53 changes: 53 additions & 0 deletions src/lib/tab-monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// observee the latest active tab
import browser from "webextension-polyfill";
import EventLite from "event-lite";

class TabMonitor extends EventLite {
constructor() {
super();
this._tab = null;
this._onActivated = this._onActivated.bind(this);
// FIXME: make it work with multiple windows
browser.tabs.onActivated.addListener(this._onActivated.bind(this));
browser.tabs.onUpdated.addListener(this._onUpdated.bind(this), {properties: ["url"]});
}

async _onActivated({tabId}) {
// FIXME:
this._tab = await browser.tabs.get(tabId);
this.emit("change");
}

_onUpdated(tabId, changeInfo, tab) {
if (!this._tab) {
return;
}
if (tabId === this._tab.id) {
this._tab = tab;
this.emit("change");
}
}

getTab() {
// if (!this._tab) {
// const tabs = await browser.tabs.query({active: true, currentWindow: true});
// this._tab = tabs[0];
// }
return this._tab;
}

destroy() {
browser.tabs.onActivated.removeListener(this._onActivated);
browser.tabs.onUpdated.removeListener(this._onUpdated);
}

isExtensionPage() {
if (!this._tab) {
return false;
}
return this._tab.url.startsWith(browser.runtime.getURL(""));
}

}

export const tabMonitor = new TabMonitor();

0 comments on commit 7add894

Please sign in to comment.