Skip to content

Commit

Permalink
Update cssinject.js
Browse files Browse the repository at this point in the history
Updated load times and added comments for clarity
  • Loading branch information
Alextimka committed Apr 14, 2024
1 parent 9441630 commit 375793a
Showing 1 changed file with 56 additions and 32 deletions.
88 changes: 56 additions & 32 deletions js/cssinject.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
document.documentElement.style.visibility = "hidden";

document.addEventListener("DOMContentLoaded", function () {
setTimeout(() => {
document.documentElement.style.visibility = "";
}, 150);
});

// Cookie functions
const setCookie = (name, value, days) => {
const expires = `; expires=${new Date(
Date.now() + days * 864e5
Expand All @@ -22,44 +15,75 @@ const getCookie = (name) => {
}
return null;
};
let themeid;
tempcookie = getCookie("themeid");
if (tempcookie == null || tempcookie == "NaN") {
setCookie("themeid", 0, 364);
themeid = 0;
} else {
themeid = parseInt(tempcookie);
}

// Switch between 0 and 1 function
// 0 - Light; 1 - Dark
const switchTheme = () => {
themeid = (themeid + 1) % 2;
setCookie("themeid", themeid, 364);
location.reload();
};

const link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = chrome.runtime.getURL("css/dark.css");
// Insert features function
function insert() {
try {
// Append theme switch
document.querySelector(".py-2").appendChild(thswitch);

const thswitch = document.createElement("a");
thswitch.classList.add("text-light", "ml-2");
thswitch.innerHTML = `<img src="${chrome.runtime.getURL(
`icons/${themeid == 1 ? "dark" : "light"}.svg`
)}">`;
thswitch.addEventListener("click", switchTheme);
// Append credit
document
.getElementsByClassName(
"d-flex align-items-center text-muted icms-links-inherit-color"
)[0]
.append(credit);
} catch {}
}

// If cookie is null or NaN reset back to 0
let themeid;
tempcookie = getCookie("themeid");
if (tempcookie == null || tempcookie == "NaN" || tempcookie > 1 || tempcookie < 0) {
setCookie("themeid", 0, 364);
themeid = 0;
} else {
themeid = parseInt(tempcookie);
}

// Append dark theme css
if (themeid == 1) {
const link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = chrome.runtime.getURL("css/dark.css");
var checkhead = setInterval(() => {
if (document.head) {
clearInterval(checkhead);
document.head.appendChild(link);
}
}, 1);
}
var checkthemeicon = setInterval(() => {
if (document.getElementsByClassName("text-light ml-2").length == 3) {
clearInterval(checkthemeicon);
document.querySelector(".py-2").appendChild(thswitch);
}
}, 1);

// Hide the page until it is fully loaded
document.documentElement.style.visibility = "hidden";

// Spectrum credit at the bottom
let credit = document.createElement("a");
credit.href = "https://github.com/Alextimka/Spectrum";
credit.target = "_blank";
credit.innerHTML = "Spectrum";

// Theme switch
const thswitch = document.createElement("a");
thswitch.classList.add("text-light", "ml-2");
thswitch.innerHTML = `<img src="${chrome.runtime.getURL(
`icons/${themeid == 1 ? "dark" : "light"}.svg`
)}">`;
thswitch.addEventListener("click", switchTheme);

// Show the page when it is fully loaded and append features
document.addEventListener("DOMContentLoaded", function () {
setTimeout(() => {
insert();
document.documentElement.style.visibility = "";
}, 150);
});

0 comments on commit 375793a

Please sign in to comment.