-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nelsonr/v1.3.1
V1.3.1
- Loading branch information
Showing
23 changed files
with
634 additions
and
633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"files.eol": "\n" | ||
"files.eol": "\n", | ||
"eslint.enable": true, | ||
"eslint.run": "onSave", | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": true, | ||
"source.organizeImports": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,92 @@ | ||
import { env } from "./utils"; | ||
|
||
function injectStylesheets(urlList: string[]) { | ||
urlList.forEach((url) => { | ||
if (!document.querySelector(`link[href="${url}"]`)) { | ||
const link = document.createElement("link"); | ||
link.rel = "stylesheet"; | ||
link.type = "text/css"; | ||
link.href = url; | ||
link.classList.add("SuperCSSInject"); | ||
|
||
document.head.appendChild(link); | ||
function main () { | ||
env.runtime.onMessage.addListener((message) => { | ||
if (message.action == "inject") { | ||
updateInjectedStylesheets(message.urlList); | ||
} | ||
}); | ||
|
||
env.runtime.sendMessage({ action: "load" }); | ||
maintainStylesheetsOrder(); | ||
} | ||
|
||
function clearStylesheets(url: string) { | ||
function updateInjectedStylesheets (urlList: string[]) { | ||
const links: NodeListOf<HTMLLinkElement> = document.querySelectorAll("link.SuperCSSInject"); | ||
|
||
if (links.length > 0) { | ||
links.forEach((link: HTMLLinkElement) => { | ||
if (link.href === url) { | ||
link.remove(); | ||
const currentList = Array.from(links).map((link) => link.href); | ||
|
||
if (currentList.length > urlList.length) { | ||
for (const url of currentList) { | ||
if (!urlList.includes(url)) { | ||
clearStylesheet(url); | ||
} | ||
}); | ||
} | ||
} else { | ||
for (const url of urlList) { | ||
if (!currentList.includes(url)) { | ||
injectStylesheet(url); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function main() { | ||
env.runtime.onMessage.addListener((message) => { | ||
if (message.action == "inject") { | ||
injectStylesheets(message.urlList); | ||
} | ||
function clearStylesheet (url: string) { | ||
const link = document.querySelector(`link[href="${url}"].SuperCSSInject`); | ||
link && link.remove(); | ||
} | ||
|
||
function injectStylesheet (url: string) { | ||
const link = createLinkElement(url); | ||
document.head.append(link); | ||
} | ||
|
||
if (message.action == "clear") { | ||
clearStylesheets(message.url); | ||
function createLinkElement (url: string) { | ||
const link = document.createElement("link"); | ||
|
||
link.rel = "stylesheet"; | ||
link.type = "text/css"; | ||
link.href = url; | ||
link.classList.add("SuperCSSInject"); | ||
|
||
return link; | ||
} | ||
|
||
/** | ||
* Make sure the injected stylesheets are always placed last on the DOM | ||
* | ||
* This handles SPAs where is common for additional assets to be loaded after | ||
* the initial page load and ensures the injected styles retain priority. | ||
*/ | ||
function maintainStylesheetsOrder () { | ||
const observer = new MutationObserver(() => { | ||
const injectedLinks: NodeListOf<HTMLLinkElement> = document.head.querySelectorAll("link.SuperCSSInject"); | ||
|
||
if (injectedLinks.length > 0) { | ||
const links: NodeListOf<HTMLLinkElement> = document.head.querySelectorAll("link[rel='stylesheet']"); | ||
const lastLink: HTMLLinkElement = links[links.length - 1]; | ||
const isInjectedStylesheetLast = lastLink.className === "SuperCSSInject"; | ||
|
||
if (!isInjectedStylesheetLast) { | ||
observer.disconnect(); | ||
moveInjectedStylesheets(); | ||
} | ||
} | ||
}); | ||
|
||
env.runtime.sendMessage({ action: "pageLoad" }); | ||
observer.observe(document.head, { childList: true }); | ||
} | ||
|
||
function moveInjectedStylesheets () { | ||
const links: NodeListOf<HTMLLinkElement> = document.head.querySelectorAll("link.SuperCSSInject"); | ||
|
||
for (const link of links) { | ||
document.head.appendChild(link); | ||
} | ||
|
||
maintainStylesheetsOrder(); | ||
} | ||
|
||
window.addEventListener("load", main); | ||
|
||
// This is just to make the TS compiler happy | ||
export {}; | ||
export { }; |
Oops, something went wrong.