Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyuyue committed Jan 13, 2024
1 parent e27dd1a commit 01a46fd
Show file tree
Hide file tree
Showing 36 changed files with 223 additions and 467 deletions.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
166 changes: 2 additions & 164 deletions extension/Background/BackgroundModule.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Import is not yet allowed in firefox, so for now I put tint_image in manifest.json
import { tint_image } from "./tint_image.js";
import { browser } from "../Vendor/Browser.js";

let BROWSERACTION_ICON = "/Images/Icon_Windowed_Mono@1x.png";
import { browser } from "../Vendor/Browser.js";

let browser_info_promise = browser.runtime.getBrowserInfo
? browser.runtime.getBrowserInfo()
Expand Down Expand Up @@ -73,7 +71,7 @@ const get_fallback_window = async (windowId) => {
// TODO Instead of using this static height, I can maybe "ping" the page I'm popup-izing
// after it is done becoming a popup: then it can figure out it's position itself
// (and check the size of it's current header itself)
const Chrome_Popup_Menubar_Height = 22; // Do `window.outerHeight - window.innerHeight` in a popup tab
const Chrome_Popup_Menubar_Height = window.outerHeight - window.innerHeight;

/**
* @typedef WindowedMode
Expand Down Expand Up @@ -184,12 +182,6 @@ onMessage("please_make_me_a_popup", async (message, sender) => {
}),
);

if (await is_firefox) {
await browser.windows.update(created_window.id, {
titlePreface: "Windowed: ",
focused: true,
});
}
return;
});

Expand Down Expand Up @@ -250,157 +242,3 @@ let ping_content_script = async (tabId) => {
delete current_port_promises[tabId];
}
};

/**
* Tries to figure out the default icon color
* - Tries to use the current theme on firefox
* - Else defaults to light and dark mode
* @param {import("webextension-polyfill-ts").Tabs.Tab} tab
* @returns {Promise<string>}
*/
let icon_theme_color = async (tab) => {
if (await is_firefox) {
let theme = await browser.theme.getCurrent(tab.windowId);
if (theme?.colors?.icons != null) {
return theme.colors.icons;
}
if (theme?.colors?.popup_text != null) {
return theme.colors.popup_text;
}
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "rgba(255,255,255,0.8)"
: "rgb(250, 247, 252)";
}

return window.matchMedia("(prefers-color-scheme: dark)").matches
? "rgba(255,255,255,0.8)"
: "#5f6368";
};

/**
* This looks a bit weird (and honestly it is) but it opens a port to the content script on a page,
* and then the page knows it should reload it's settings.
* TODO? Should I close the port?
* @param {number} tabId
* @param {any} properties
*/
let notify_tab_state = async (tabId, properties) => {
let port = browser.tabs.connect(tabId);
// port.postMessage(JSON.stringify({ method: 'notify', data: properties }))
};

/**
* Shorthand for setting the browser action icon on a tab
* @param {number} tabId
* @param {{ icon: ImageData, title: string }} action
*/
let apply_browser_action = async (tabId, action) => {
await browser.browserAction.setIcon({
tabId: tabId,
imageData: action.icon,
});
await browser.browserAction.setTitle({
tabId: tabId,
title: action.title,
});
};

/**
* @param {import("webextension-polyfill-ts").Tabs.Tab} tab
*/
let update_button_on_tab = async (tab) => {
let has_contentscript_active =
tab.status === "complete" && (await ping_content_script(tab.id));

// A specific exception for about:blank so, on firefox,
// when you customize your menu bar, windowed is at it's most beautiful.
if (has_contentscript_active === false && tab.url === "about:blank") {
await apply_browser_action(tab.id, {
icon: await tint_image(BROWSERACTION_ICON, await icon_theme_color(tab)),
title: `Windowed`,
});
return;
}

// On some domains windowed will never work, because it is blocked by the browser.
// To avoid user confusion with the "You have to reload" message,
// I show a descriptive error 💁‍♀️
if (
has_contentscript_active === false &&
(tab.url.match(/^about:/) ||
tab.url.match(/^chrome:\/\//) ||
tab.url.match(/^edge:\/\//) ||
tab.url.match(/^https?:\/\/chrome\.google\.com/) ||
tab.url.match(/^https?:\/\/support\.mozilla\.org/))
) {
await apply_browser_action(tab.id, {
icon: await tint_image(BROWSERACTION_ICON, "rgba(208, 2, 27, .22)"),
title: `For security reasons, windowed is not supported on this domain (${tab.url}).`,
});
return;
}

// So if the tab is loaded, and it is not an extra secure domain,
// it means windowed is not loaded for some reason. So I tell that.
if (tab.status === "complete" && has_contentscript_active === false) {
await apply_browser_action(tab.id, {
icon: await tint_image(BROWSERACTION_ICON, "#D0021B"),
title:
"This page needs to be reloaded for Windowed to activate. Click here to reload.",
});
return;
}

// From here I figure out what the user has configured for Windowed on this domain,
// and show a specific icon and title for each of those.
let host = new URL(tab.url).host;
let config = await get_host_config(tab);
if (config.mode === "fullscreen" && config.pip === false) {
// ONLY FULLSCREEN - Dimmed icon because it is basically disabled
await apply_browser_action(tab.id, {
icon: await tint_image(BROWSERACTION_ICON, "rgba(133, 133, 133, 0.5)"),
title: `Windowed is disabled on ${host}, click to re-activate`,
});
await notify_tab_state(tab.id, { disabled: true });
} else if (config.mode === "ask" && config.pip === false) {
// ONLY ASK - Normal white icon because this is "normal"
await apply_browser_action(tab.id, {
icon: await tint_image(BROWSERACTION_ICON, await icon_theme_color(tab)),
title: `Windowed is enabled on ${host}`,
});
await notify_tab_state(tab.id, { disabled: false });
} else {
// SOMETHING SPECIFIC - Light blue because now the extension is really changing your browsing explicitly
await apply_browser_action(tab.id, {
icon: await tint_image(BROWSERACTION_ICON, "#16a8a8"),
title: `Windowed is enabled on ${host}`,
});
await notify_tab_state(tab.id, { disabled: false });
}
};

// Events where I refresh the browser action button
browser.runtime.onInstalled.addListener(async () => {
let all_tabs = await browser.tabs.query({});
for (let tab of all_tabs) {
await update_button_on_tab(tab);
}
});
browser.tabs.onUpdated.addListener(async (tabId, changed, tab) => {
if (changed.url != null || changed.status != null) {
await update_button_on_tab(tab);
}
});
// Not sure if I need this one -
// only reason I need it is for when one would toggle Enabled/Disabled
browser.tabs.onActivated.addListener(async ({ tabId }) => {
let tab = await browser.tabs.get(tabId);
await update_button_on_tab(tab);
});
// Because I load this as a module now, I am making sure this code is ran as much as possible
(async () => {
let all_tabs = await browser.tabs.query({});
for (let tab of all_tabs) {
await update_button_on_tab(tab);
}
})();
106 changes: 0 additions & 106 deletions extension/Background/tint_image.js

This file was deleted.

Loading

0 comments on commit 01a46fd

Please sign in to comment.