use throw for cleaner code

This commit is contained in:
Niki Wix Skaarup 2025-04-02 23:04:55 +02:00
parent c4e857822e
commit 9d9eda0b23
Signed by: nikiskaarup
GPG key ID: FC2F1B116F6E788C

View file

@ -2,7 +2,7 @@
// @name manga reading // @name manga reading
// @namespace https://userscripts.skaarup.dev // @namespace https://userscripts.skaarup.dev
// @homepageURL https://userscripts.skaarup.dev // @homepageURL https://userscripts.skaarup.dev
// @version 3.1 // @version 3.2
// @author nws // @author nws
// @description Adds nearly complete keyboard navigation and cleans up the user interface of manganato // @description Adds nearly complete keyboard navigation and cleans up the user interface of manganato
// @updateURL https://userscripts.skaarup.dev/scripts/manga-reading.user.js // @updateURL https://userscripts.skaarup.dev/scripts/manga-reading.user.js
@ -57,6 +57,8 @@ import mangaReadingNotificationContainer from './templates/manga-reading-notific
import mangaReadingNotification from './templates/manga-reading-notification.html'; import mangaReadingNotification from './templates/manga-reading-notification.html';
async function initMangaReading() { async function initMangaReading() {
const logName = `${GM.info.script.name} -`;
function disableScrolling() { function disableScrolling() {
document.documentElement.style.top = `${-document.documentElement.scrollTop}px`; document.documentElement.style.top = `${-document.documentElement.scrollTop}px`;
document.documentElement.style.position = 'fixed'; document.documentElement.style.position = 'fixed';
@ -74,10 +76,10 @@ async function initMangaReading() {
document.documentElement.scrollTop = scrollTop; document.documentElement.scrollTop = scrollTop;
document.body.scrollTop = scrollTop; document.body.scrollTop = scrollTop;
} }
function loadStyle(name: string, inline: boolean, shadowRoot: ShadowRoot | null) { function loadStyle(name: string, inline: boolean, shadowRoot: ShadowRoot) {
const data = GM_getResourceText(name); const data = GM_getResourceText(name);
if (!data) { if (!data) {
console.warn(`Resource ${name} not found`); console.warn(`${logName} Resource ${name} not found`);
return; return;
} }
@ -88,7 +90,7 @@ async function initMangaReading() {
style.innerHTML = data; style.innerHTML = data;
if (inline) document.head.append(style); if (inline) document.head.append(style);
else shadowRoot?.appendChild(style); else shadowRoot.appendChild(style);
} }
function getMr() { function getMr() {
@ -128,19 +130,20 @@ async function initMangaReading() {
); );
const outerFrame = document.createElement('mr-config-container'); const outerFrame = document.createElement('mr-config-container');
if (!outerFrame.shadowRoot) throw new Error('Shadow root not found');
const shadowRoot = outerFrame.shadowRoot; const shadowRoot = outerFrame.shadowRoot;
if (!shadowRoot) return;
loadStyle('stylesheet', false, shadowRoot); loadStyle('stylesheet', false, shadowRoot);
const frame = shadowRoot.children[0]; const frame = shadowRoot.children[0];
if (!frame) return; if (!frame) throw new Error('Frame not found');
const backdrop = frame.querySelector<HTMLDivElement>('[data-mr-backdrop]'); const backdrop = frame.querySelector<HTMLDivElement>('[data-mr-backdrop]');
if (!backdrop) return; if (!backdrop) throw new Error('Backdrop not found');
backdrop.onclick = closeConfig; backdrop.onclick = closeConfig;
const subConfigTarget = frame.querySelector<HTMLDivElement>('[data-mr-sub-config-target]'); const subConfigTarget = frame.querySelector<HTMLDivElement>('[data-mr-sub-config-target]');
if (!subConfigTarget) throw new Error('Sub config target not found');
const closeConfigButtons = frame.querySelectorAll<HTMLButtonElement>('[data-mr-close]'); const closeConfigButtons = frame.querySelectorAll<HTMLButtonElement>('[data-mr-close]');
for (let i = 0; i < closeConfigButtons.length; i++) { for (let i = 0; i < closeConfigButtons.length; i++) {
@ -173,7 +176,7 @@ async function initMangaReading() {
callback callback
}; };
ui.subConfigTarget?.appendChild(container); ui.subConfigTarget.appendChild(container);
} }
function unregisterConfig(info: vm_infoType) { function unregisterConfig(info: vm_infoType) {
@ -200,7 +203,7 @@ async function initMangaReading() {
disableScrolling(); disableScrolling();
shadowRoot?.querySelector<HTMLInputElement>('input')?.focus(); shadowRoot.querySelector<HTMLInputElement>('input')?.focus();
} }
function closeConfig() { function closeConfig() {
@ -242,9 +245,9 @@ async function initMangaReading() {
} }
function attachFocusEvent() { function attachFocusEvent() {
console.log(`${GM.info.script.name} - Attaching focus event...`); console.log(`${logName} Attaching focus event...`);
ui.frame?.addEventListener('keydown', keydownEventListener as any); ui.frame.addEventListener('keydown', keydownEventListener as any);
console.log(`${GM.info.script.name} - Attached focus events.`); console.log(`${logName} Attached focus events.`);
} }
function noModifier(e: KeyboardEvent) { function noModifier(e: KeyboardEvent) {
@ -263,19 +266,6 @@ async function initMangaReading() {
return e.altKey && !(e.ctrlKey || e.shiftKey); return e.altKey && !(e.ctrlKey || e.shiftKey);
} }
async function globalShortcuts(event: KeyboardEvent) {
switch (true) {
case event.code === 'Backslash' && ctrlModifier(event):
// setDebugging(!configGlobals.debugging);
// ui.debuggingCheckbox.checked = configGlobals.debugging;
return true;
case event.code === 'Semicolon' && ctrlModifier(event):
// reloadResources();
return true;
}
return false;
}
async function configOpenShortcuts(event: KeyboardEvent) { async function configOpenShortcuts(event: KeyboardEvent) {
if (event.code === 'Escape' && noModifier(event)) { if (event.code === 'Escape' && noModifier(event)) {
closeConfig(); closeConfig();
@ -316,9 +306,9 @@ async function initMangaReading() {
} }
function attachShortcutEvents() { function attachShortcutEvents() {
console.log(`${GM.info.script.name} - Attaching shortcut events...`); console.log(`${logName} Attaching shortcut events...`);
document.addEventListener('keyup', keyupEventListener); document.addEventListener('keyup', keyupEventListener);
console.log(`${GM.info.script.name} - Attached shortcut events.`); console.log(`${logName} Attached shortcut events.`);
} }
function registerKeyUp(shortcutType: shortcutType, ...shortcuts: shortcut[]) { function registerKeyUp(shortcutType: shortcutType, ...shortcuts: shortcut[]) {
@ -337,42 +327,37 @@ async function initMangaReading() {
} }
function registerKeyUps() { function registerKeyUps() {
const namePrefix = 'nwsLib';
registerKeyUp('Global', {
name: `${namePrefix} - global`,
callback: globalShortcuts
});
registerKeyUp('ConfigOpen', { registerKeyUp('ConfigOpen', {
name: `${namePrefix} - config open`, name: `${logName} config open`,
callback: configOpenShortcuts callback: configOpenShortcuts
}); });
registerKeyUp('ConfigClosed', { registerKeyUp('ConfigClosed', {
name: `${namePrefix} - config closed`, name: `${logName} config closed`,
callback: configClosedShortcuts callback: configClosedShortcuts
}); });
} }
async function onInit(callback: () => Promise<void>, postCallback: () => Promise<void>) { async function onInit(callback: () => Promise<void>, postCallback: () => Promise<void>) {
try { try {
console.log(`${GM.info.script.name} - Loading...`); console.log(`${logName} Loading...`);
const id = GM.registerMenuCommand(`Configure ${GM.info.script.name}`, () => { const id = GM.registerMenuCommand(`Configure ${GM.info.script.name}`, () => {
openConfig(); openConfig();
}); });
registerKeyUps(); registerKeyUps();
attachFocusEvent(); attachFocusEvent();
attachShortcutEvents(); attachShortcutEvents();
console.log(`${GM.info.script.name} - Loaded.`); console.log(`${logName} Loaded.`);
await callback(); await callback();
if (postCallback !== undefined) { if (postCallback !== undefined) {
await postCallback(); await postCallback();
} }
} catch (e) { } catch (e) {
console.error(`${GM.info.script.name} - Error:`, e); console.error(`${logName} Error:`, e);
} }
} }
async function init(callback: () => Promise<void>, postCallback: () => Promise<void>) { async function init(callback: () => Promise<void>, postCallback: () => Promise<void>) {
console.log(`${GM.info.script.name} - Initializing...`); console.log(`${logName} Initializing...`);
switch (document.readyState) { switch (document.readyState) {
case 'complete': case 'complete':
await onInit(callback, postCallback); await onInit(callback, postCallback);
@ -412,7 +397,6 @@ async function initMangaReading() {
}; };
} }
const mr = getMr(); const mr = getMr();
if (!mr) return;
const globals: manga_reading.globalsType = { const globals: manga_reading.globalsType = {
nextUrl: '', nextUrl: '',
@ -461,22 +445,23 @@ async function initMangaReading() {
notificationList.style.setProperty('--z-index', '999999999'); notificationList.style.setProperty('--z-index', '999999999');
function initToastContainer() { function initToastContainer() {
console.log(`${GM.info.script.name} - Initializing Toast Container.`); console.log(`${logName} Initializing Toast Container.`);
if (!mr) return;
for (let i = 0; i < mr.shadowRoot.children.length; i++) { for (let i = 0; i < mr.shadowRoot.children.length; i++) {
const element = mr.shadowRoot.children[i]; const element = mr.shadowRoot.children[i];
if (element?.tagName.toLowerCase() === 'style') { if (!element) continue;
if (element.tagName.toLowerCase() === 'style') {
shadowRoot.appendChild(element.cloneNode(true)); shadowRoot.appendChild(element.cloneNode(true));
} }
} }
console.log(`${GM.info.script.name} - Initialized Toast Container.`); console.log(`${logName} Initialized Toast Container.`);
} }
function insertToastContainer() { function insertToastContainer() {
console.log(`${GM.info.script.name} - Inserting Toast Container.`); console.log(`${logName} Inserting Toast Container.`);
document.body.appendChild(outerFrame); document.body.appendChild(outerFrame);
console.log(`${GM.info.script.name} - Inserted Toast Container.`); console.log(`${logName} Inserted Toast Container.`);
} }
const notificationTemp = document.createElement('div'); const notificationTemp = document.createElement('div');
@ -489,21 +474,39 @@ async function initMangaReading() {
const notificationButton = notificationButtonTemp.children[0] as HTMLDivElement; const notificationButton = notificationButtonTemp.children[0] as HTMLDivElement;
const temp = document.createElement('div'); const temp = document.createElement('div');
temp.innerHTML = atob(mangaReadingConfig); temp.innerHTML = atob(mangaReadingConfig);
const configElement = temp.children[0] as HTMLDivElement; const configElement = temp.children[0] as HTMLDivElement;
const ptApiUrl = configElement.querySelector<HTMLInputElement>('[data-mr-pt-api-url]');
if (!ptApiUrl) throw new Error('PT API URL not found');
const ptApiBearerToken = configElement.querySelector<HTMLInputElement>(
'[data-mr-pt-api-bearer-token]'
);
if (!ptApiBearerToken) throw new Error('PT API Bearer Token not found');
const titleList = configElement.querySelector<HTMLTextAreaElement>('[data-mr-title-list]');
if (!titleList) throw new Error('Title List not found');
const subTitle = configElement.querySelector<HTMLSpanElement>('[data-mr-title-current]');
if (!subTitle) throw new Error('Subtitle not found');
const btnAdd = configElement.querySelector<HTMLButtonElement>('[data-mr-title-current-add]');
if (!btnAdd) throw new Error('Add button not found');
const btnRemove = configElement.querySelector<HTMLButtonElement>(
'[data-mr-title-current-remove]'
);
if (!btnRemove) throw new Error('Remove button not found');
const btnSave = configElement.querySelector<HTMLButtonElement>('[data-mr-title-list-save]');
if (!btnSave) throw new Error('Save button not found');
const btnReset = configElement.querySelector<HTMLButtonElement>('[data-mr-title-list-reset]');
if (!btnReset) throw new Error('Reset button not found');
const ui = { const ui = {
ptApiUrl: configElement.querySelector<HTMLInputElement>('[data-mr-pt-api-url]'), ptApiUrl,
ptApiBearerToken: configElement.querySelector<HTMLInputElement>( ptApiBearerToken,
'[data-mr-pt-api-bearer-token]' titleList,
), subTitle,
titleList: configElement.querySelector<HTMLTextAreaElement>('[data-mr-title-list]'), btnAdd,
subTitle: configElement.querySelector<HTMLSpanElement>('[data-mr-title-current]'), btnRemove,
btnAdd: configElement.querySelector<HTMLButtonElement>('[data-mr-title-current-add]'), btnSave,
btnRemove: configElement.querySelector<HTMLButtonElement>('[data-mr-title-current-remove]'), btnReset
btnSave: configElement.querySelector<HTMLButtonElement>('[data-mr-title-list-save]'),
btnReset: configElement.querySelector<HTMLButtonElement>('[data-mr-title-list-reset]')
}; };
function escapeRegExp(input: string) { function escapeRegExp(input: string) {
@ -513,13 +516,12 @@ async function initMangaReading() {
window.location = url as string & Location; window.location = url as string & Location;
} }
function setTitleList() { function setTitleList() {
if (!ui.titleList) return;
ui.titleList.value = globals.titleList.join('\r\n'); ui.titleList.value = globals.titleList.join('\r\n');
} }
function setPTUi() { function setPTUi() {
if (ui.ptApiUrl) ui.ptApiUrl.value = globals.ptApi.url; ui.ptApiUrl.value = globals.ptApi.url;
if (ui.ptApiBearerToken) ui.ptApiBearerToken.value = globals.ptApi.bearerToken; ui.ptApiBearerToken.value = globals.ptApi.bearerToken;
} }
function atNeither() { function atNeither() {
@ -565,6 +567,7 @@ async function initMangaReading() {
for (let i = 0; i < images.length; i++) { for (let i = 0; i < images.length; i++) {
const image = images[i]; const image = images[i];
if (!image) continue; if (!image) continue;
image.style.width = `${(pageWidth / image.width) * 100}%`; image.style.width = `${(pageWidth / image.width) * 100}%`;
} }
@ -575,8 +578,9 @@ async function initMangaReading() {
for (let i = 0; i < images.length; i++) { for (let i = 0; i < images.length; i++) {
const image = images[i]; const image = images[i];
if (!image) continue; if (!image) continue;
image.style.width = `${(pageWidth / image.width) * 100}%`; image.style.width = `${(pageWidth / image.width) * 100}%`;
image.style.width = `${(window?.visualViewport?.height ?? 1 / image.height) * 100}%`; image.style.width = `${(window.visualViewport?.height ?? 1 / image.height) * 100}%`;
} }
return; return;
@ -585,22 +589,17 @@ async function initMangaReading() {
for (let i = 0; i < images.length; i++) { for (let i = 0; i < images.length; i++) {
const image = images[i]; const image = images[i];
if (!image) continue; if (!image) continue;
image.style.width = image.style.width = `${input}%`; image.style.width = image.style.width = `${input}%`;
} }
} }
function setSubTitle() { function setSubTitle() {
if (!ui.subTitle) return;
ui.subTitle.innerText = atChapterOrManga() ? globals.currentTitle : 'No title'; ui.subTitle.innerText = atChapterOrManga() ? globals.currentTitle : 'No title';
} }
function registerConfig() { function registerConfig() {
setSubTitle(); setSubTitle();
if (!mr) return;
if (!ui.btnRemove) return;
if (!ui.btnAdd) return;
if (!ui.btnReset) return;
if (!ui.btnSave) return;
ui.btnRemove.onclick = removeTitle; ui.btnRemove.onclick = removeTitle;
ui.btnRemove.disabled = atNeither(); ui.btnRemove.disabled = atNeither();
@ -609,7 +608,6 @@ async function initMangaReading() {
ui.btnAdd.disabled = atNeither(); ui.btnAdd.disabled = atNeither();
ui.btnReset.onclick = () => { ui.btnReset.onclick = () => {
if (!ui.titleList) return;
ui.titleList.value = globals.titleList.join('\r\n'); ui.titleList.value = globals.titleList.join('\r\n');
}; };
@ -622,28 +620,26 @@ async function initMangaReading() {
setTitleList(); setTitleList();
setPTUi(); setPTUi();
if (!ui.btnRemove) return;
if (!ui.btnAdd) return;
ui.btnRemove.disabled = atNeither(); ui.btnRemove.disabled = atNeither();
ui.btnAdd.disabled = atNeither(); ui.btnAdd.disabled = atNeither();
}); });
} }
function addTitle() { function addTitle() {
const trimmedValue = ui.titleList?.value.trim() ?? ''; const trimmedValue = ui.titleList.value.trim();
const taTitleListValue = trimmedValue.split(/\r?\n/); const taTitleListValue = trimmedValue.split(/\r?\n/);
if (taTitleListValue.includes(globals.currentTitle)) return; if (taTitleListValue.includes(globals.currentTitle)) {
return;
}
const curTAVal = trimmedValue.length > 0 ? `${trimmedValue}\r\n` : ''; const curTAVal = trimmedValue.length > 0 ? `${trimmedValue}\r\n` : '';
if (!ui.titleList) return;
ui.titleList.value = curTAVal + globals.currentTitle; ui.titleList.value = curTAVal + globals.currentTitle;
} }
async function saveTitles() { async function saveTitles() {
globals.titleList = [...new Set(ui.titleList?.value.trim().split(/\r?\n/).sort())]; globals.titleList = [...new Set(ui.titleList.value.trim().split(/\r?\n/).sort())];
await GM.setValue(key.titleList, JSON.stringify(globals.titleList)); await GM.setValue(key.titleList, JSON.stringify(globals.titleList));
if (atChapter()) { if (atChapter()) {
@ -652,15 +648,13 @@ async function initMangaReading() {
} }
async function savePtApi() { async function savePtApi() {
globals.ptApi.url = ui.ptApiUrl?.value.trim() ?? ''; globals.ptApi.url = ui.ptApiUrl.value.trim();
globals.ptApi.bearerToken = ui.ptApiBearerToken?.value.trim() ?? ''; globals.ptApi.bearerToken = ui.ptApiBearerToken.value.trim();
await GM.setValue(key.ptAPi, JSON.stringify(globals.ptApi)); await GM.setValue(key.ptAPi, JSON.stringify(globals.ptApi));
} }
function removeTitle() { function removeTitle() {
if (!ui.titleList) return; const curTAVal = ui.titleList.value.trim();
const curTAVal = ui.titleList.value.trim() ?? '';
const regex = new RegExp(`${escapeRegExp(globals.currentTitle)}\\r?\\n?`, 'gi'); const regex = new RegExp(`${escapeRegExp(globals.currentTitle)}\\r?\\n?`, 'gi');
ui.titleList.value = curTAVal.replace(regex, ''); ui.titleList.value = curTAVal.replace(regex, '');
} }
@ -679,6 +673,7 @@ async function initMangaReading() {
const latestChapterLink = latestChapter?.querySelector<HTMLAnchorElement>('& > span > a'); const latestChapterLink = latestChapter?.querySelector<HTMLAnchorElement>('& > span > a');
if (!latestChapterLink) return; if (!latestChapterLink) return;
setLocation(latestChapterLink.href); setLocation(latestChapterLink.href);
} }
@ -695,7 +690,7 @@ async function initMangaReading() {
} }
function findUrls() { function findUrls() {
console.log(`${GM.info.script.name} - Finding URLs...`); console.log(`${logName} Finding URLs...`);
const links = document.querySelectorAll<HTMLAnchorElement>('div.breadcrumb > p > span > a'); const links = document.querySelectorAll<HTMLAnchorElement>('div.breadcrumb > p > span > a');
const titleLink = links[1]; const titleLink = links[1];
@ -705,7 +700,7 @@ async function initMangaReading() {
setSubTitle(); setSubTitle();
if (!atChapter() || titleLink === undefined) { if (!atChapter() || titleLink === undefined) {
console.log(`${GM.info.script.name} - Found URLs.`); console.log(`${logName} Found URLs.`);
return; return;
} }
@ -729,39 +724,39 @@ async function initMangaReading() {
globals.prevUrl = titleLink.href; globals.prevUrl = titleLink.href;
} }
console.log(`${GM.info.script.name} - Found URLs.`); console.log(`${logName} Found URLs.`);
} }
async function loadTitleList() { async function loadTitleList() {
console.log(`${GM.info.script.name} - Loading title list...`); console.log(`${logName} Loading title list...`);
const value = await GM.getValue(key.titleList, JSON.stringify({ titleList: [] })); const value = await GM.getValue(key.titleList, JSON.stringify({ titleList: [] }));
if (typeof value !== 'string') { if (typeof value !== 'string') {
console.error('Invalid titleList value:', value); console.error(`${logName} Invalid titleList value: ${value}`);
globals.titleList = []; globals.titleList = [];
return; return;
} }
globals.titleList = JSON.parse(value); globals.titleList = JSON.parse(value);
console.log(`${GM.info.script.name} - Loaded title list.`); console.log(`${logName} Loaded title list.`);
} }
async function loadPtApi() { async function loadPtApi() {
console.log(`${GM.info.script.name} - Loading progress tracker api...`); console.log(`${logName} Loading progress tracker api...`);
const value = await GM.getValue(key.ptAPi, JSON.stringify({ url: '', bearerToken: '' })); const value = await GM.getValue(key.ptAPi, JSON.stringify({ url: '', bearerToken: '' }));
if (typeof value !== 'string') { if (typeof value !== 'string') {
console.error('Invalid ptApi value:', value); console.error(`${logName} Invalid ptApi value: ${value}`);
globals.ptApi = { url: '', bearerToken: '' }; globals.ptApi = { url: '', bearerToken: '' };
return; return;
} }
globals.ptApi = JSON.parse(value); globals.ptApi = JSON.parse(value);
console.log(`${GM.info.script.name} - Loaded progress tracker api.`); console.log(`${logName} Loaded progress tracker api.`);
} }
function manganatoSiteOverrides() { function manganatoSiteOverrides() {
@ -793,9 +788,9 @@ async function initMangaReading() {
} }
function siteOverrides() { function siteOverrides() {
console.log(`${GM.info.script.name} - Applying site overrides...`); console.log(`${logName} Applying site overrides...`);
manganatoSiteOverrides(); manganatoSiteOverrides();
console.log(`${GM.info.script.name} - Applied site overrides.`); console.log(`${logName} Applied site overrides.`);
} }
function setAt() { function setAt() {
@ -804,7 +799,7 @@ async function initMangaReading() {
const atChapter = new RegExp(/\/manga\/[\w.\-~%]+\/chapter-[\d.-]+/).test(path); const atChapter = new RegExp(/\/manga\/[\w.\-~%]+\/chapter-[\d.-]+/).test(path);
const atManga = new RegExp(/\/manga\/[\w.\-~%]+$/).test(path); const atManga = new RegExp(/\/manga\/[\w.\-~%]+$/).test(path);
console.log(`${GM.info.script.name} - Setting at...`, { atChapter, atManga }); console.log(`${logName} Setting at...`, { atChapter, atManga });
if (atChapter) globals.at = 'chapter'; if (atChapter) globals.at = 'chapter';
else if (atManga) globals.at = 'manga'; else if (atManga) globals.at = 'manga';
@ -820,7 +815,6 @@ async function initMangaReading() {
'.panel_page_number > .group_page > a.page_select' '.panel_page_number > .group_page > a.page_select'
); );
console.log('pageSelected', pageSelected);
if (!pageSelected) return; if (!pageSelected) return;
const previous = pageSelected.previousElementSibling as HTMLAnchorElement | null; const previous = pageSelected.previousElementSibling as HTMLAnchorElement | null;
@ -960,13 +954,13 @@ async function initMangaReading() {
}); });
if (!response.ok) { if (!response.ok) {
console.error('Failed to fetch bookmarks'); console.error(`${logName} Failed to fetch bookmarks`);
return []; return [];
} }
return response.json(); return response.json();
} catch (e) { } catch (e) {
console.error('Failed to fetch bookmarks'); console.error(`${logName} Failed to fetch bookmarks`);
console.error(e); console.error(e);
return []; return [];
} }
@ -980,12 +974,12 @@ async function initMangaReading() {
}); });
if (!response.ok) { if (!response.ok) {
console.error('Failed to delete bookmark'); console.error(`${logName} Failed to delete bookmark`);
return false; return false;
} }
return true; return true;
} catch (e) { } catch (e) {
console.error('Failed to delete bookmarks'); console.error(`${logName} Failed to delete bookmarks`);
console.error(e); console.error(e);
return false; return false;
} }
@ -996,7 +990,7 @@ async function initMangaReading() {
body: { name: string; href: string } body: { name: string; href: string }
) { ) {
try { try {
console.log(`${GM.info.script.name} - Creating or updating bookmark`, options.input, body); console.log(`${logName} Creating or updating bookmark`, options.input, body);
const response = await nwsFetch(options.input, { const response = await nwsFetch(options.input, {
method: 'PUT', method: 'PUT',
body: JSON.stringify(body), body: JSON.stringify(body),
@ -1004,7 +998,7 @@ async function initMangaReading() {
}); });
if (!response.ok) { if (!response.ok) {
console.error(`${GM.info.script.name} - Failed to create or update bookmark`); console.error(`${logName} Failed to create or update bookmark`);
return { success: false, created: false }; return { success: false, created: false };
} }
@ -1013,12 +1007,12 @@ async function initMangaReading() {
json.success = true; json.success = true;
return json; return json;
} catch (e) { } catch (e) {
console.error(`${GM.info.script.name} - Failed to parse response`); console.error(`${logName} Failed to parse response`);
console.error(e); console.error(e);
return { success: false, created: false }; return { success: false, created: false };
} }
} catch (e) { } catch (e) {
console.error(`${GM.info.script.name} - Failed to create or update bookmarks`); console.error(`${logName} Failed to create or update bookmarks`);
console.error(e); console.error(e);
return { success: false, created: false }; return { success: false, created: false };
} }
@ -1036,12 +1030,12 @@ async function initMangaReading() {
}); });
if (!response.ok) { if (!response.ok) {
console.error(`${GM.info.script.name} - Failed to check bookmark`); console.error(`${logName} Failed to check bookmark`);
return false; return false;
} }
return true; return true;
} catch (e) { } catch (e) {
console.error(`${GM.info.script.name} - Failed to check bookmark`); console.error(`${logName} Failed to check bookmark`);
console.error(e); console.error(e);
return false; return false;
} }
@ -1099,7 +1093,7 @@ async function initMangaReading() {
const content = notification.querySelector<HTMLDivElement>('[data-content]'); const content = notification.querySelector<HTMLDivElement>('[data-content]');
if (title === null || description === null || content === null) { if (title === null || description === null || content === null) {
console.error(`${GM.info.script.name} - Notification elements not found.`); console.error(`${logName} Notification elements not found.`);
return; return;
} }
@ -1362,28 +1356,25 @@ async function initMangaReading() {
} }
function registerKeyUps() { function registerKeyUps() {
mr?.shortcut.keyUp.register('ConfigClosed', { mr.shortcut.keyUp.register('ConfigClosed', {
name: `${GM.info.script.name} - config closed`, name: `${logName} config closed`,
callback: configClosedShortcuts callback: configClosedShortcuts
}); });
} }
async function checkFirstRun() { async function checkFirstRun() {
console.log(`${GM.info.script.name} - First run check...`); console.log(`${logName} First run check...`);
const value = await GM.getValue(key.firstRun, true); const value = await GM.getValue(key.firstRun, true);
if (value) { if (value) {
console.log(`${GM.info.script.name} - First run detected.`); console.log(`${logName} First run detected.`);
GM.setValue(key.firstRun, false); GM.setValue(key.firstRun, false);
GM.notification( GM.notification('First run setup complete', `${logName} ${GM.info.script.name}`);
'First run setup complete',
`${GM.info.script.name} - ${GM.info.script.name}`
);
} }
console.log(`${GM.info.script.name} - First run checked.`); console.log(`${logName} First run checked.`);
} }
async function onInit() { async function onInit() {
console.log(`${GM.info.script.name} - ${GM.info.script.name} - Loading...`); console.log(`${logName} ${GM.info.script.name} - Loading...`);
await checkFirstRun(); await checkFirstRun();
registerKeyUps(); registerKeyUps();
setAt(); setAt();
@ -1394,19 +1385,27 @@ async function initMangaReading() {
removeMargins(); removeMargins();
} }
siteOverrides(); siteOverrides();
console.log(`${GM.info.script.name} - ${GM.info.script.name} - Loaded.`); console.log(`${logName} ${GM.info.script.name} - Loaded.`);
} }
async function postInit() { async function postInit() {
console.log(`${GM.info.script.name} - ${GM.info.script.name} - Post Loading...`); console.log(`${logName} ${GM.info.script.name} - Post Loading...`);
initToastContainer(); initToastContainer();
insertToastContainer(); insertToastContainer();
loadStyle('stylesheet', false, shadowRoot); loadStyle('stylesheet', false, shadowRoot);
loadStyle('overrides', true, shadowRoot); loadStyle('overrides', true, shadowRoot);
console.log(`${GM.info.script.name} - ${GM.info.script.name} - Post Loaded.`); console.log(`${logName} ${GM.info.script.name} - Post Loaded.`);
} }
registerConfig(); registerConfig();
mr.init(onInit, postInit); mr.init(onInit, postInit);
} }
initMangaReading(); async function mangaReadingSafeStart() {
try {
await initMangaReading();
} catch (e) {
console.error(`Error initializing userscript - ${GM.info.script.name}:\n`, e);
}
}
mangaReadingSafeStart();