Skip to content

Commit

Permalink
Improve loader smoothness
Browse files Browse the repository at this point in the history
  • Loading branch information
mstieranka committed Oct 22, 2023
1 parent 2f06873 commit 963f164
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
3 changes: 0 additions & 3 deletions src/components/loader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
let isVisible = true;
function showLoader() {
document.querySelectorAll("dialog").forEach((el) => el.remove());
console.log("showLoader", isVisible);
isVisible = true;
}
function hideLoader() {
console.log("hideLoader", isVisible);
isVisible = false;
}
Expand Down
22 changes: 13 additions & 9 deletions src/content/end.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const main = async (settings: ExtensionSettings) => {
};

const replaceStyles = (theme: string) => {
if (theme === "orig") return Promise.resolve();

document.head.querySelectorAll('link[href$="/css.css"]').forEach((e) => {
e.remove();
});
Expand All @@ -114,20 +116,22 @@ const replaceStyles = (theme: string) => {
chrome.runtime.getURL("themes/" + theme + ".css"),
);
document.getElementsByTagName("head")[0].appendChild(link);
return new Promise((resolve) => {
link.onload = resolve;
});
};

chrome.runtime.sendMessage(
{ type: MessageType.GET_SETTINGS },
async (settings: ExtensionSettings) => {
console.log("PTT end with settings:", settings);
if (settings.theme === "orig") {
return;
}
replaceStyles(settings.theme);
if (settings.theme === "orig-dark") {
setTimeout(() => document.dispatchEvent(pttLoadedEvent), 0);
return;
}
await main(settings).then(() => document.dispatchEvent(pttLoadedEvent));
await Promise.all([
replaceStyles(settings.theme).then(() => {
console.log("PTT styles loaded");
}),
!["orig", "orig-dark"].includes(settings.theme) && main(settings),
]);
console.log("PTT loaded");
document.dispatchEvent(pttLoadedEvent);
},
);
11 changes: 5 additions & 6 deletions src/content/loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Loader from "../components/loader.svelte";

const loaderElement = document.getElementById("ptt-loader");
if (loaderElement) {
new Loader({
target: loaderElement,
});
}
const loaderElement = document.createElement("pttloader");
document.documentElement.appendChild(loaderElement);
new Loader({
target: loaderElement,
});
7 changes: 0 additions & 7 deletions src/content/start.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { pttLoadedEvent } from "../events";
import { MessageType } from "../messages";
import { ExtensionSettings } from "../settings";

Expand All @@ -19,12 +18,7 @@ const setFavicon = () => {
};

const addLoader = () => {
const div = document.createElement("pttloader");
div.id = "ptt-loader";
document.documentElement.appendChild(div);

const script = document.createElement("script");
script.id = "ptt-loader-script";
script.setAttribute("type", "text/javascript");
script.setAttribute("src", chrome.runtime.getURL("content/loader.js"));
document.documentElement.appendChild(script);
Expand All @@ -39,7 +33,6 @@ chrome.runtime.sendMessage(
}
console.log("PTT start with settings:", settings);
if (settings.theme == "orig" || settings.theme == "orig-dark") {
setTimeout(() => document.dispatchEvent(pttLoadedEvent), 0);
return;
}

Expand Down

0 comments on commit 963f164

Please sign in to comment.