Skip to content

Commit

Permalink
Add "beta" badge, set icon on chrome restart
Browse files Browse the repository at this point in the history
  • Loading branch information
aiden2480 committed Apr 26, 2022
1 parent f5b2834 commit 7842cdf
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,41 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {(async

})(); return true});

/* Set up a listener for when the extension is installed */
/* Set up a listener for when the extension is installed/chrome restarts */
chrome.runtime.onInstalled.addListener(async reason => {
console.log("Install event fired with", reason);
chrome.runtime.setUninstallURL("https://kanjithing-backend.chocolatejade42.repl.co/uninstall");

if ((await chrome.management.getSelf()).installType !== "development")
chrome.runtime.setUninstallURL("https://kanjithing-backend.chocolatejade42.repl.co/uninstall");

await ensureDefaultConfiguration();
await ensureCorrectKanjiIcon();
await ensureBetaBadge();
});

chrome.runtime.onStartup.addListener(async () => {
await ensureDefaultConfiguration();
await ensureCorrectKanjiIcon();
await ensureBetaBadge();
});

/* Configuration functions called above */
async function ensureCorrectKanjiIcon() {
var { customsets, selectedset, selectedkanji } = await chrome.storage.local.get();
if ([ customsets, selectedset, selectedkanji ].includes(undefined)) return;

setBrowserIcon(customsets[selectedset].kanji[selectedkanji], bypass=true);
}

async function ensureBetaBadge() {
// Ensure that the "Beta" badge is present if necessary

if ((await chrome.management.getSelf()).installType === "development") {
chrome.action.setBadgeText({ text: "B" });
chrome.action.setBadgeBackgroundColor({ color: "#304db6" });
}
}

async function ensureDefaultConfiguration() {
// Create default sets
var sets = (await chrome.storage.local.get("customsets")).customsets;
Expand All @@ -66,9 +93,9 @@ async function ensureDefaultConfiguration() {
}

/* Script to change the browser icon */
function setBrowserIcon(kanji) {
function setBrowserIcon(kanji, bypass=false) {
// https://jsfiddle.net/1u37ovj9/
if (current === kanji) return;
if (current === kanji && !bypass) return;

var canvas = new OffscreenCanvas(64, 64);
var context = canvas.getContext("2d");
Expand All @@ -85,9 +112,10 @@ function setBrowserIcon(kanji) {

current = kanji;
var imageData = context.getImageData(0, 0, 64, 64);
chrome.action.setIcon({imageData}, () => console.log(`Set browser icon to %c${kanji}`, "color: #7289da"));
chrome.action.setIcon({ imageData }, () => console.log(`Set browser icon to %c${kanji}`, "color: #7289da"));
}

/* Creates defult configuration as required by ensureDefaultConfiguration */
async function createKanjiSets() {
// {id: ..., name: ..., kanji: ..., enabled: ...}

Expand All @@ -102,7 +130,7 @@ async function createKanjiSets() {
}

async function createDefaultConfig() {
var { videoSpeed, settingsbtn} = await chrome.storage.local.get(["videoSpeed", "settingsbtn"]);
var { videoSpeed, settingsbtn } = await chrome.storage.local.get(["videoSpeed", "settingsbtn"]);
(videoSpeed !== undefined) || await chrome.storage.local.set({ videoSpeed: 0.8 });
(settingsbtn !== undefined) || await chrome.storage.local.set({ settingsbtn: true });
}

0 comments on commit 7842cdf

Please sign in to comment.