mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-29 16:44:13 +08:00
verygood
This commit is contained in:
parent
010579f20b
commit
28face8da2
@ -1,5 +1,5 @@
|
|||||||
from translator.basetranslator_dev import basetransdev
|
from translator.basetranslator_dev import basetransdev
|
||||||
import time
|
import time, os
|
||||||
|
|
||||||
|
|
||||||
class TS(basetransdev):
|
class TS(basetransdev):
|
||||||
@ -24,27 +24,23 @@ class TS(basetransdev):
|
|||||||
"th": "Thai",
|
"th": "Thai",
|
||||||
}
|
}
|
||||||
|
|
||||||
def inittranslator(self):
|
def injectjs(self):
|
||||||
self.currenttext = None
|
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
|
||||||
|
return
|
||||||
def getcurr(self, idx):
|
with open(
|
||||||
|
os.path.join(os.path.dirname(__file__), "hookchatgptfetch.js"),
|
||||||
res = self.wait_for_result(
|
"r",
|
||||||
r"""document.querySelector("#__next > div.relative.z-0.flex.h-full.w-full.overflow-hidden > div.relative.flex.h-full.max-w-full.flex-1.flex-col.overflow-hidden > main > div.flex.h-full.flex-col > div.flex-1.overflow-hidden > div > div > div > div > div:nth-child({}) > div > div > div.relative.flex.w-full.flex-col.agent-turn > div.flex-col.gap-1.md\\:gap-3 > div.flex.flex-grow.flex-col.max-w-full > div > div").textContent""".format(
|
encoding="utf8",
|
||||||
idx + 2
|
) as ff:
|
||||||
)
|
js = ff.read()
|
||||||
)
|
self.Runtime_evaluate(js)
|
||||||
if "This content may violate our usage policies." == res:
|
self.Runtime_evaluate("window.injectedjs=true")
|
||||||
raise Exception("This content may violate our usage policies.")
|
|
||||||
return res
|
|
||||||
|
|
||||||
def translate(self, content):
|
def translate(self, content):
|
||||||
while True:
|
self.injectjs()
|
||||||
idx = self.wait_for_result(
|
|
||||||
"""document.querySelector("#__next > div.relative.z-0.flex.h-full.w-full.overflow-hidden > div.relative.flex.h-full.max-w-full.flex-1.flex-col.overflow-hidden > main > div.flex.h-full.flex-col > div.flex-1.overflow-hidden > div > div > div > div").children.length"""
|
self.Runtime_evaluate("hasdone=false")
|
||||||
)
|
self.Runtime_evaluate('thistext=""')
|
||||||
if isinstance(idx, int):
|
|
||||||
break
|
|
||||||
content = (
|
content = (
|
||||||
"You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format(
|
"You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format(
|
||||||
self.srclang, self.tgtlang
|
self.srclang, self.tgtlang
|
||||||
@ -57,27 +53,20 @@ class TS(basetransdev):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.Runtime_evaluate(
|
self.Runtime_evaluate(
|
||||||
r"""document.querySelector("#__next > div.relative.z-0.flex.h-full.w-full.overflow-hidden > div.relative.flex.h-full.max-w-full.flex-1.flex-col.overflow-hidden > main > div.flex.h-full.flex-col.focus-visible\\:outline-0 > div.w-full.md\\:pt-0.dark\\:border-white\\/20.md\\:border-transparent.md\\:dark\\:border-transparent.md\\:w-\\[calc\\(100\\%-\\.5rem\\)\\].juice\\:w-full > div.px-3.text-base.md\\:px-4.m-auto.md\\:px-5.lg\\:px-1.xl\\:px-5 > div > form > div > div.flex.w-full.items-center > div > button").click()"""
|
r"""document.querySelector("#__next > div.relative.z-0.flex.h-full.w-full.overflow-hidden > div > main > div.flex.h-full.flex-col.focus-visible\\:outline-0 > div.md\\:pt-0.dark\\:border-white\\/20.md\\:border-transparent.md\\:dark\\:border-transparent.w-full > div.text-base.px-3.md\\:px-4.m-auto.md\\:px-5.lg\\:px-1.xl\\:px-5 > div > form > div > div.flex.w-full.items-center > div > div > button").click()"""
|
||||||
)
|
)
|
||||||
self.currenttext = content
|
if self.config["usingstream"]:
|
||||||
currtext = ""
|
curr = ""
|
||||||
if self.tgtlang == "Arabic":
|
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
|
||||||
|
time.sleep(0.1)
|
||||||
while self.currenttext == content:
|
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
|
||||||
time.sleep(1) # get text before violate usage policies.
|
if thistext.startswith(curr):
|
||||||
|
yield thistext[len(curr) :]
|
||||||
newcurr = self.getcurr(idx)
|
else:
|
||||||
needbreak = newcurr == currtext and newcurr != ""
|
yield thistext
|
||||||
currtext = newcurr
|
curr = thistext
|
||||||
if needbreak:
|
|
||||||
break
|
|
||||||
yield currtext
|
|
||||||
else:
|
else:
|
||||||
while self.currenttext == content:
|
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
|
||||||
time.sleep(0.01) # get text before violate usage policies.
|
time.sleep(0.1)
|
||||||
|
continue
|
||||||
newcurr = self.getcurr(idx)
|
yield self.Runtime_evaluate("thistext")["result"]["value"]
|
||||||
if newcurr == currtext:
|
|
||||||
continue
|
|
||||||
yield newcurr[len(currtext) :]
|
|
||||||
currtext = newcurr
|
|
||||||
|
67
LunaTranslator/LunaTranslator/translator/hookchatgptfetch.js
Normal file
67
LunaTranslator/LunaTranslator/translator/hookchatgptfetch.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
|
||||||
|
const originalFetch = window.fetch;
|
||||||
|
var hasdone = true
|
||||||
|
var thistext = ''
|
||||||
|
window.fetch = function (input, init) {
|
||||||
|
const fetchPromise = originalFetch.apply(this, arguments);
|
||||||
|
if (!input.includes("conversation")) return fetchPromise;
|
||||||
|
hasdone = false;
|
||||||
|
thistext = ''
|
||||||
|
// 构造一个自定义的可读流
|
||||||
|
//controller.close()会导致真正的fetch被终止
|
||||||
|
// 不太会js,只能让他假等待了,ui里面会显示在等待,但luna里面能读到,而且按钮也能正确按下
|
||||||
|
const customReadableStream = new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
setTimeout(() => {
|
||||||
|
|
||||||
|
controller.close();
|
||||||
|
}, 99999999999);
|
||||||
|
|
||||||
|
},
|
||||||
|
});
|
||||||
|
fetchPromise.then(response => {
|
||||||
|
const contentType = response.headers.get('content-type');
|
||||||
|
|
||||||
|
if (contentType && contentType.includes('text/event-stream')) {
|
||||||
|
console.log(response.body)
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
|
||||||
|
reader.read().then(function processStream({ done, value }) {
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
hasdone = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let lines = new TextDecoder('utf-8').decode(value);
|
||||||
|
lines = lines.split('\n')
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
let line = lines[i]
|
||||||
|
line = line.trim()
|
||||||
|
if (line.length == 0) continue;
|
||||||
|
try {
|
||||||
|
const chunk = JSON.parse(line.substring(6));
|
||||||
|
console.log("Chunk received:", chunk.message.content.parts[0]);
|
||||||
|
thistext = chunk.message.content.parts[0]
|
||||||
|
} catch {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return reader.read().then(processStream);
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('Error reading stream:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('Fetch error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
resolve(new Response(customReadableStream, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'Content-type': 'text/event-stream'
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
});
|
||||||
|
};
|
@ -1570,7 +1570,8 @@
|
|||||||
"use": false,
|
"use": false,
|
||||||
"color": "blue",
|
"color": "blue",
|
||||||
"type": "dev",
|
"type": "dev",
|
||||||
"name": "chatgpt"
|
"name": "chatgpt",
|
||||||
|
"is_gpt_like": true
|
||||||
},
|
},
|
||||||
"deepl_dev": {
|
"deepl_dev": {
|
||||||
"use": false,
|
"use": false,
|
||||||
|
@ -732,6 +732,17 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"dev_chatgpt": {
|
||||||
|
"args": {
|
||||||
|
"usingstream": true
|
||||||
|
},
|
||||||
|
"argstype": {
|
||||||
|
"usingstream": {
|
||||||
|
"name": "流式输出",
|
||||||
|
"type": "switch"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"gemini": {
|
"gemini": {
|
||||||
"args": {
|
"args": {
|
||||||
"注册网址": "https://ai.google.dev/tutorials/python_quickstart",
|
"注册网址": "https://ai.google.dev/tutorials/python_quickstart",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user