mirror of
https://github.com/NohamR/Pocket-Bird.git
synced 2026-05-25 04:07:23 +00:00
Add browser extension context
This commit is contained in:
100
src/context.js
100
src/context.js
@@ -1,6 +1,8 @@
|
||||
|
||||
import { debug, log, error } from "./shared.js";
|
||||
|
||||
const SAVE_KEY = "birbSaveData";
|
||||
|
||||
/**
|
||||
* @typedef {import('./application.js').BirbSaveData} BirbSaveData
|
||||
*/
|
||||
@@ -20,9 +22,9 @@ export class Context {
|
||||
|
||||
/**
|
||||
* @abstract
|
||||
* @returns {BirbSaveData|{}}
|
||||
* @returns {Promise<BirbSaveData|{}>}
|
||||
*/
|
||||
getSaveData() {
|
||||
async getSaveData() {
|
||||
throw new Error("Method not implemented");
|
||||
}
|
||||
|
||||
@@ -30,7 +32,7 @@ export class Context {
|
||||
* @abstract
|
||||
* @param {BirbSaveData} saveData
|
||||
*/
|
||||
putSaveData(saveData) {
|
||||
async putSaveData(saveData) {
|
||||
throw new Error("Method not implemented");
|
||||
}
|
||||
|
||||
@@ -44,45 +46,62 @@ export class Context {
|
||||
|
||||
export class LocalContext extends Context {
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isContextActive() {
|
||||
return window.location.hostname === "127.0.0.1"
|
||||
|| window.location.hostname === "localhost"
|
||||
|| window.location.hostname.startsWith("192.168.");
|
||||
}
|
||||
|
||||
getSaveData() {
|
||||
/**
|
||||
* @override
|
||||
* @returns {Promise<BirbSaveData|{}>}
|
||||
*/
|
||||
async getSaveData() {
|
||||
log("Loading save data from localStorage");
|
||||
return JSON.parse(localStorage.getItem("birbSaveData") ?? "{}");
|
||||
return JSON.parse(localStorage.getItem(SAVE_KEY) ?? "{}");
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @param {BirbSaveData} saveData
|
||||
*/
|
||||
putSaveData(saveData) {
|
||||
async putSaveData(saveData) {
|
||||
log("Saving data to localStorage");
|
||||
localStorage.setItem("birbSaveData", JSON.stringify(saveData));
|
||||
localStorage.setItem(SAVE_KEY, JSON.stringify(saveData));
|
||||
}
|
||||
|
||||
/** @override */
|
||||
resetSaveData() {
|
||||
log("Resetting save data in localStorage");
|
||||
localStorage.removeItem("birbSaveData");
|
||||
localStorage.removeItem(SAVE_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
export class UserScriptContext extends Context {
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isContextActive() {
|
||||
// @ts-expect-error
|
||||
return typeof GM_getValue === "function";
|
||||
}
|
||||
|
||||
getSaveData() {
|
||||
/**
|
||||
* @override
|
||||
* @returns {Promise<BirbSaveData|{}>}
|
||||
*/
|
||||
async getSaveData() {
|
||||
log("Loading save data from UserScript storage");
|
||||
/** @type {BirbSaveData|{}} */
|
||||
let saveData = {};
|
||||
// @ts-expect-error
|
||||
saveData = GM_getValue("birbSaveData", {}) ?? {};
|
||||
saveData = GM_getValue(SAVE_KEY, {}) ?? {};
|
||||
return saveData;
|
||||
}
|
||||
|
||||
@@ -90,22 +109,75 @@ export class UserScriptContext extends Context {
|
||||
* @override
|
||||
* @param {BirbSaveData} saveData
|
||||
*/
|
||||
putSaveData(saveData) {
|
||||
async putSaveData(saveData) {
|
||||
log("Saving data to UserScript storage");
|
||||
// @ts-expect-error
|
||||
GM_setValue("birbSaveData", saveData);
|
||||
GM_setValue(SAVE_KEY, saveData);
|
||||
}
|
||||
|
||||
/** @override */
|
||||
resetSaveData() {
|
||||
log("Resetting save data in UserScript storage");
|
||||
// @ts-expect-error
|
||||
GM_deleteValue("birbSaveData");
|
||||
GM_deleteValue(SAVE_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
class BrowserExtensionContext extends Context {
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isContextActive() {
|
||||
// @ts-expect-error
|
||||
return typeof chrome !== "undefined";
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @returns {Promise<BirbSaveData|{}>}
|
||||
*/
|
||||
async getSaveData() {
|
||||
log("Loading save data from browser extension storage");
|
||||
return new Promise((resolve) => {
|
||||
// @ts-expect-error
|
||||
chrome.storage.sync.get([SAVE_KEY], (result) => {
|
||||
resolve(result[SAVE_KEY] ?? {});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @param {BirbSaveData} saveData
|
||||
*/
|
||||
async putSaveData(saveData) {
|
||||
log("Saving data to browser extension storage");
|
||||
// @ts-expect-error
|
||||
chrome.storage.sync.set({ [SAVE_KEY]: saveData }, function () {
|
||||
// @ts-expect-error
|
||||
if (chrome.runtime.lastError) {
|
||||
// @ts-expect-error
|
||||
console.error(chrome.runtime.lastError);
|
||||
} else {
|
||||
console.log("Settings saved successfully");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @override */
|
||||
resetSaveData() {
|
||||
log("Resetting save data in browser extension storage");
|
||||
// @ts-expect-error
|
||||
chrome.storage.sync.clear();
|
||||
}
|
||||
}
|
||||
|
||||
const CONTEXTS = [
|
||||
new LocalContext(),
|
||||
new UserScriptContext(),
|
||||
new BrowserExtensionContext(),
|
||||
new LocalContext()
|
||||
];
|
||||
|
||||
export function getContext() {
|
||||
|
||||
Reference in New Issue
Block a user