Skip to content

Commit

Permalink
feat: update worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Reamd7 committed Sep 7, 2021
1 parent e09ece6 commit 0a54a29
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
File renamed without changes.
88 changes: 88 additions & 0 deletions worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const BaseUrl = "自定义cloudflare worker 域名"
const NotionUrl = "www.notion.so";
const BasePath = `https://${BaseUrl}`;
const NotionPath = `https://${NotionUrl}`;

addEventListener('fetch', function(event) {
event.respondWith(handleRequest(event.request))
})

class ElementHandler {
async element(element) {
const res = await fetch("https://cdn.jsdelivr.net/gh/Reamd7/notion-zh_CN@main/json/zh.json").then(res => {
return res.text();
})
element.prepend(`
<script id="messages" type="application/json" data-locale="zh-CN">
${res}
</script>
`, {
html: true
})
}
}
class ScriptHandler {
element(element) {
element.append(`
<script>
window.CONFIG.domainBaseUrl = "${BasePath}"
</script>
`, {
html: true
})
}
}

async function handleRequest(request) {

const method = request.method;
const headers = new Headers(request.headers);
headers.set("origin", NotionPath)
headers.set("referer", NotionPath);
headers.set("host", NotionPath);

const destinationURL = request.url.replace(BaseUrl, NotionUrl);
if (method === "GET") {
const headers = request.headers;
return fetch(destinationURL, {
method: request.method,
headers: headers,
}).then(rep => {
const contentType = rep.headers.get("Content-Type")
if (contentType.startsWith("text/html")) {
return new HTMLRewriter().on("head", new ElementHandler()).on("body", new ScriptHandler()).transform(rep)
}
return rep;
})
} else {
let selfheader = {};
headers.forEach((v, k) => {
selfheader[k] = v;
})
selfheader = {
...selfheader,
origin: NotionPath,
referer: NotionPath,
host: NotionPath,
}
const newR = new Request(destinationURL, {
...request,
method: request.method,
headers: selfheader,
body: request.body,
});
return fetch(newR).then(rep => {
let selfheader = {};
rep.headers.forEach((v, k) => {
selfheader[k] = v.replaceAll(NotionUrl, BaseUrl);
})
return new Response(rep.body, {
status: rep.status,
statusText: rep.statusText,
headers: selfheader
})
})
}
}


0 comments on commit 0a54a29

Please sign in to comment.