From f710d007ea91e65767d401fac80b9d2f2aad4e0a Mon Sep 17 00:00:00 2001 From: FoxRefire <155989196+FoxRefire@users.noreply.github.com> Date: Sun, 28 Apr 2024 05:58:48 +0900 Subject: [PATCH] Fix corsFetch --- content.js | 15 ++++++++------- manifest.json | 3 ++- popup.js | 10 +++++++--- python/schemes/RedBee.py | 11 ++++++----- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/content.js b/content.js index a27b513..b98d7d2 100644 --- a/content.js +++ b/content.js @@ -20,18 +20,19 @@ document.addEventListener('pssh', (e) => { //Fetch from original origin chrome.runtime.onMessage.addListener( - async function (request, sender, sendResponse) { + function (request, sender, sendResponse) { if(request.type=="FETCH"){ - console.log("DEBUG") - let res = await fetch(request.u, { + console.log("DEBUG:"+JSON.stringify(request)) + let res = fetch(request.u, { method: request.m, headers: request.h, body: request.b - }).then((r)=>r.json()).then((r)=>{ - btoa(String.fromCharCode(...new Uint8Array(r))) + }).then((r)=>r.arrayBuffer()).then((r)=>{ + sendResponse( + btoa(String.fromCharCode(...new Uint8Array(r))) + ); }) - sendResponse({res: res}); - return true } + return true } ); diff --git a/manifest.json b/manifest.json index d381166..273fa2c 100644 --- a/manifest.json +++ b/manifest.json @@ -11,7 +11,8 @@ "", "activeTab", "windows", - "storage" + "storage", + "tabs" ], "background": { "scripts": ["background.js"], diff --git a/popup.js b/popup.js index 7535d69..1d04baa 100644 --- a/popup.js +++ b/popup.js @@ -34,9 +34,13 @@ function copyResult(){ } window.corsFetch = (u, m, h, b) => { - chrome.runtime.sendMessage({type:"FETCH", u:u, m:m, h:h, b:b}, function(response) { - console.log(response) - }); + return new Promise((resolve, reject) => { + chrome.tabs.query({ url:pageURL }, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, {type:"FETCH", u:u, m:m, h:h, b:b}, (res) => { + resolve(res) + }) + }) + }) } if(psshs.length!=0){ diff --git a/python/schemes/RedBee.py b/python/schemes/RedBee.py index 3a3e00a..ac0cece 100644 --- a/python/schemes/RedBee.py +++ b/python/schemes/RedBee.py @@ -1,14 +1,15 @@ import base64 -import re import js -req = base64.b64decode(licBody.encode()).decode() +from pyodide.ffi import to_js +req = json.loads(base64.b64decode(licBody.encode()).decode()) b64challenge = base64.b64encode(challenge).decode() -req = re.sub(r'(?<=\"message\":\").*(?=\"})', b64challenge, req) -print(req) +req['message'] = b64challenge # res = await (await pyfetch(licUrl, # method="POST", # headers=licHeaders, # body=req # )).json() -res = js.window.corsFetch(licUrl, "POST", licHeaders, req) +res = await js.corsFetch(licUrl, "POST", to_js(licHeaders), json.dumps(req)) +res = json.loads(base64.b64decode(res.encode()).decode()) +print(res) licence = base64.b64decode(res['license'].encode())