🍙 more bug fixes

This commit is contained in:
Shiba 2022-08-09 17:49:16 +00:00
parent 15f03b3bcc
commit 6af354de0c
4 changed files with 40 additions and 41 deletions

View file

@ -68,6 +68,7 @@ export function Dialogs(params: { manager: ReturnType<typeof createDialogManager
{` {`
.alert-dialog .alert-dialog
{ {
position: fixed;
border: none; border: none;
background: var(--color-dark); background: var(--color-dark);
color: var(--color-light); color: var(--color-light);

View file

@ -1,25 +1,21 @@
// This should only work in background // This should only work in background
let db: IDBDatabase | null = null
if (typeof chrome.extension === 'undefined') throw new Error("YT urlCache can only be accessed from extension windows and service-workers.") if (typeof chrome.extension === 'undefined') throw new Error("YT urlCache can only be accessed from extension windows and service-workers.")
let db = new Promise<IDBDatabase>((resolve, reject) => {
if (typeof self.indexedDB !== 'undefined') { if (typeof self.indexedDB !== 'undefined') {
const openRequest = indexedDB.open("yt-url-resolver-cache") const openRequest = indexedDB.open("yt-url-resolver-cache")
openRequest.addEventListener('upgradeneeded', () => openRequest.result.createObjectStore("store").createIndex("expireAt", "expireAt")) openRequest.addEventListener('upgradeneeded', () => openRequest.result.createObjectStore("store").createIndex("expireAt", "expireAt"))
// Delete Expired
openRequest.addEventListener('success', () => { openRequest.addEventListener('success', () => {
db = openRequest.result resolve(openRequest.result)
clearExpired() clearExpired()
}) }, { once: true })
} }
else console.warn(`IndexedDB not supported`) else reject(`IndexedDB not supported`)
})
async function clearExpired() { async function clearExpired() {
return new Promise<void>((resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
if (!db) throw new Error(`IDBDatabase not defined.`) const transaction = (await db).transaction("store", "readwrite")
const transaction = db.transaction("store", "readwrite")
const range = IDBKeyRange.upperBound(new Date()) const range = IDBKeyRange.upperBound(new Date())
const expireAtCursorRequest = transaction.objectStore("store").index("expireAt").openCursor(range) const expireAtCursorRequest = transaction.objectStore("store").index("expireAt").openCursor(range)
@ -40,8 +36,8 @@ async function clearExpired() {
} }
async function clearAll() { async function clearAll() {
return await new Promise<void>((resolve, reject) => { return await new Promise<void>(async (resolve, reject) => {
const store = db?.transaction("store", "readwrite").objectStore("store") const store = (await db).transaction("store", "readwrite").objectStore("store")
if (!store) return resolve() if (!store) return resolve()
const request = store.clear() const request = store.clear()
request.addEventListener('success', () => resolve()) request.addEventListener('success', () => resolve())
@ -50,8 +46,8 @@ async function clearAll() {
} }
async function put(url: string | null, id: string): Promise<void> { async function put(url: string | null, id: string): Promise<void> {
return await new Promise((resolve, reject) => { return await new Promise(async (resolve, reject) => {
const store = db?.transaction("store", "readwrite").objectStore("store") const store = (await db).transaction("store", "readwrite").objectStore("store")
if (!store) return resolve() if (!store) return resolve()
const expireAt = !url ? new Date(Date.now() + 1 * 60 * 60 * 1000) : new Date(Date.now() + 15 * 24 * 60 * 60 * 1000) const expireAt = !url ? new Date(Date.now() + 1 * 60 * 60 * 1000) : new Date(Date.now() + 15 * 24 * 60 * 60 * 1000)
const request = store.put({ value: url, expireAt }, id) const request = store.put({ value: url, expireAt }, id)
@ -65,8 +61,8 @@ async function put(url: string | null, id: string): Promise<void> {
// null means there is cache of that id has no lbrypathname // null means there is cache of that id has no lbrypathname
// undefined means there is no cache // undefined means there is no cache
async function get(id: string): Promise<string | null | undefined> { async function get(id: string): Promise<string | null | undefined> {
const response = (await new Promise((resolve, reject) => { const response = (await new Promise(async (resolve, reject) => {
const store = db?.transaction("store", "readonly").objectStore("store") const store = (await db).transaction("store", "readonly").objectStore("store")
if (!store) return reject(`Can't find object store.`) if (!store) return reject(`Can't find object store.`)
const request = store.get(id) const request = store.get(id)

View file

@ -228,11 +228,13 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
console.error("Background error on:", params) console.error("Background error on:", params)
throw new Error(`Background error. ${response ?? ''}`) throw new Error(`Background error. ${response ?? ''}`)
} }
console.log(response)
return response ? JSON.parse(response) : null return response ? JSON.parse(response) : null
} }
// Request new tab // Request new tab
async function openNewTab(url: URL, active: boolean) { async function openNewTab(url: URL, active: boolean) {
if (!open(url.href, '_blank'))
chrome.runtime.sendMessage({ method: 'openTab', data: JSON.stringify({ href: url.href, active }) }) chrome.runtime.sendMessage({ method: 'openTab', data: JSON.stringify({ href: url.href, active }) })
} }
@ -246,7 +248,7 @@ import { getExtensionSettingsAsync, getSourcePlatfromSettingsFromHostname, getTa
let urlHrefCache: string | null = null let urlHrefCache: string | null = null
while (true) { while (true) {
await sleep(500) await sleep(500)
const url: URL = new URL(location.href); const url: URL = new URL(location.href)
await (async () => { await (async () => {
const source = await getSourceByUrl(new URL(location.href)) const source = await getSourceByUrl(new URL(location.href))