import { decode } from 'html-entities';
-
-class SetBase {
- constructor(origin) {
- this.origin = origin;
- }
-
- element(element) {
- element.prepend(`<base href="${this.origin}" />`, { html: true });
- }
-}
-
-class UpdateLink {
- element(element) {
- for (const attrName of ['href', 'src']) {
- const attrValue = element.getAttribute(attrName);
- if (attrValue !== null && attrValue[0] === '/') {
- element.setAttribute(attrName, attrValue.substring(1));
- }
- }
- }
-}
+import { SetBase, UpdateLink } from '../proxy';
class ReplaceBitcoin {
#buffer = '';
+import { SetBase, UpdateLink } from '../proxy';
+
class InjectEruda {
element(element) {
element.prepend('<script src="//cdn.jsdelivr.net/npm/eruda"></script><script>eruda.init();</script>', { html: true });
export default {
async fetch(request) {
const url = /^http(s)?:\/\//i.test(request.url) ? new URL(request.url) : new URL(`https://${request.url}`);
- const queryString = decodeURIComponent(url.search.substring(1));
+ const remoteUrl = new URL(decodeURIComponent(url.search.substring(1)));
- const response = await fetch(queryString);
- return new HTMLRewriter().on('body', new InjectEruda()).transform(response);
+ const response = await fetch(remoteUrl);
+ return new HTMLRewriter()
+ .on('head', new SetBase(remoteUrl.origin))
+ .on('link, script', new UpdateLink())
+ .on('body', new InjectEruda())
+ .transform(response);
}
}
+export class SetBase {
+ constructor(origin) {
+ this.origin = origin;
+ }
+
+ element(element) {
+ element.prepend(`<base href="${this.origin}" />`, { html: true });
+ }
+}
+
+export class UpdateLink {
+ element(element) {
+ for (const attrName of ['href', 'src']) {
+ const attrValue = element.getAttribute(attrName);
+ if (attrValue !== null && attrValue[0] === '/') {
+ element.setAttribute(attrName, attrValue.substring(1));
+ }
+ }
+ }
+}
+
export default {
async fetch(request) {
const url = /^http(s)?:\/\//i.test(request.url) ? new URL(request.url) : new URL(`https://${request.url}`);
- const queryString = decodeURIComponent(url.search.substring(1));
- return await fetch(queryString);
+ const remoteUrl = new URL(decodeURIComponent(url.search.substring(1)));
+ const response = await fetch(remoteUrl);
+ return new HTMLRewriter()
+ .on('head', new SetBase(remoteUrl.origin))
+ .on('link, script', new UpdateLink())
+ .transform(response);
}
}
+import { SetBase, UpdateLink } from '../proxy';
+
class InjectStyle {
element(element) {
element.prepend(`<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/sakura.css@1.4.1/css/sakura.min.css">`, { html: true });
export default {
async fetch(request) {
const url = /^http(s)?:\/\//i.test(request.url) ? new URL(request.url) : new URL(`https://${request.url}`);
- const queryString = decodeURIComponent(url.search.substring(1));
+ const remoteUrl = new URL(decodeURIComponent(url.search.substring(1)));
- const response = await fetch(queryString);
- return new HTMLRewriter().on('html', new InjectStyle()).transform(response);
+ const response = await fetch(remoteUrl);
+ return new HTMLRewriter()
+ .on('head', new SetBase(remoteUrl.origin))
+ .on('link, script', new UpdateLink())
+ .on('html', new InjectStyle())
+ .transform(response);
}
}