diff --git a/src/shared/rewriters/css.ts b/src/shared/rewriters/css.ts index b09668a..55f65b0 100644 --- a/src/shared/rewriters/css.ts +++ b/src/shared/rewriters/css.ts @@ -1,64 +1,42 @@ -// This CSS rewriter uses code from Meteor -// You can find the original source code at https://github.com/MeteorProxy/Meteor - import { URLMeta, rewriteUrl, unrewriteUrl } from "./url"; export function rewriteCss(css: string, meta: URLMeta) { - const regex = - /(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g; - - return css.replace( - regex, - ( - match, - importStatement, - urlQuote, - urlContent, - importQuote, - importContent - ) => { - const url = urlContent || importContent; - const encodedUrl = rewriteUrl(url.trim(), meta); - - if (importStatement) { - return `@import url(${urlQuote}${encodedUrl}${urlQuote})`; - } - - if (importQuote) { - return `@import ${importQuote}${encodedUrl}${importQuote}`; - } - - return `url(${urlQuote}${encodedUrl}${urlQuote})`; - } - ); + handleCss("unrewrite", css, meta); } export function unrewriteCss(css: string) { - const regex = - /(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g; - - return css.replace( - regex, - ( - match, - importStatement, - urlQuote, - urlContent, - importQuote, - importContent - ) => { - const url = urlContent || importContent; - const encodedUrl = unrewriteUrl(url.trim()); - - if (importStatement) { - return `@import url(${urlQuote}${encodedUrl}${urlQuote})`; - } - - if (importQuote) { - return `@import ${importQuote}${encodedUrl}${importQuote}`; - } - - return `url(${urlQuote}${encodedUrl}${urlQuote})`; - } - ); + handleCss("rewrite", css); +} + +function handleCss(type: "rewrite" | "unrewrite", css: string, meta?: URLMeta) { + const urlRegex = /url\(['"]?(.+?)['"]?\)/gm; + const Atruleregex = + /@import\s+(url\s*?\(.{0,9999}?\)|['"].{0,9999}?['"]|.{0,9999}?)($|\s|;)/gm; + css = new String(css).toString(); + css = css.replace(urlRegex, (match, url) => { + const encodedUrl = + type === "rewrite" + ? rewriteUrl(url.trim(), meta) + : unrewriteUrl(url.trim()); + + return match.replace(url, encodedUrl); + }); + css = css.replace(Atruleregex, (_, importStatement) => { + return importStatement.replace( + /^(url\(['"]?|['"]|)(.+?)(['"]|['"]?\)|)$/gm, + (match, firstQuote, url, endQuote) => { + if (firstQuote.startsWith("url")) { + return match; + } + const encodedUrl = + type === "rewrite" + ? rewriteUrl(url.trim(), meta) + : unrewriteUrl(url.trim()); + + return `${firstQuote}${encodedUrl}${endQuote}`; + } + ); + }); + + return css; } diff --git a/src/worker/error.ts b/src/worker/error.ts index 525b34a..03e458c 100644 --- a/src/worker/error.ts +++ b/src/worker/error.ts @@ -12,138 +12,137 @@ export function errorTemplate(trace: string, fetchedURL: string) { `; return ` - - - - Scramjet - - - -
-
-

Uh oh!

-

There was an error loading

- - -
- -
-

Try:

-
    -
  • Checking your internet connection
  • -
  • Verifying you entered the correct address
  • -
  • Clearing the site data
  • -
  • Contacting 's administrator
  • -
  • Verify the server isn't censored
  • -
-

If you're the administrator of , try:

-
    -
  • Restarting your server
  • -
  • Updating Scramjet
  • -
  • Troubleshooting the error on the GitHub repository
  • -
-
-
-
- -
-

Scramjet v (build )

- - - + #version-wrapper { + width: 100%; + text-align: center; + position: absolute; + bottom: 0.2rem; + font-size: 0.8rem; + color: var(--shore)!important; + i { + background-color: color-mix(in srgb, var(--deep), transparent 50%); + border-radius: 9999px; + padding: 0.2em 0.5em; + } + z-index: 101; + } + + + +
+
+

Uh oh!

+

There was an error loading

+ +
+ +
+

Try:

+
    +
  • Checking your internet connection
  • +
  • Verifying you entered the correct address
  • +
  • Clearing the site data
  • +
  • Contacting 's administrator
  • +
  • Verify the server isn't censored
  • +
+

If you're the administrator of , try:

+
    +
  • Restarting your server
  • +
  • Updating Scramjet
  • +
  • Troubleshooting the error on the GitHub repository
  • +
+
+
+
+ +
+

Scramjet v (build )

+ + + `; }