This commit is contained in:
恍兮惚兮 2024-07-24 23:11:37 +08:00
parent b20e3517f2
commit 09f11f2a32
9 changed files with 185 additions and 423 deletions

View File

@ -1,5 +1,5 @@
from translator.basetranslator import basetrans
import json, requests
import json, requests, threading
from myutils.config import globalconfig
import websocket, time
from traceback import print_exc
@ -38,12 +38,14 @@ class basetransdev(basetrans):
#########################################
def _private_init(self):
self._id = 1
self.sendrecvlock = threading.Lock()
self._createtarget()
super()._private_init()
def _SendRequest(self, method, params, ws=None):
if self.using == False:
return
with self.sendrecvlock:
self._id += 1
if ws is None:
@ -55,11 +57,8 @@ class basetransdev(basetrans):
try:
return res["result"]
except:
if res["error"]["code"] == -32600:
self._id += 1
self._SendRequest(method, params, ws)
else:
raise
print(res)
raise Exception()
def _createtarget(self):
if self.using == False:

View File

@ -1,12 +1,14 @@
(function () {
window.hasdone = false
window.thistext = ''
if (window.injectedjs) return
window.injectedjs = true
const originalFetch = window.fetch;
var hasdone = true
var thistext = ''
window.fetch = function (input, init) {
const fetchPromise = originalFetch.apply(this, arguments);
if (!%s) return fetchPromise;
hasdone = false;
thistext = ''
window.hasdone = false;
window.thistext = ''
fetchPromise.then(response => {
const clone = response.clone()
const contentType = clone.headers.get('content-type');
@ -42,3 +44,4 @@ window.fetch = function (input, init) {
return fetchPromise;
};
})()

View File

@ -1,6 +1,8 @@
(function () {
window.hasdone = false
window.thistext = ''
if (window.injectedjs) return
const originXHR = XMLHttpRequest
var hasdone = true
var thistext = ''
window.XMLHttpRequest = function () {
var newxhr = new originXHR()
newxhr.open_ori = newxhr.open
@ -8,8 +10,8 @@ window.XMLHttpRequest = function () {
newxhr.open = function () {
var input = arguments[1]
if (%s) {
hasdone = false;
thistext = ''
window.hasdone = false;
window.thistext = ''
newxhr.onprogress_ori = newxhr.onprogress
newxhr.onload_ori = newxhr.onload_ori
newxhr.offset = 0
@ -42,3 +44,4 @@ window.XMLHttpRequest = function () {
return newxhr
}
})()

View File

@ -1,76 +1,19 @@
from translator.basetranslator_dev import basetransdev
import time, os
from translator.dev_llm_common import commonllmdev
class TS(basetransdev):
class TS(commonllmdev):
target_url = "https://chatglm.cn/main/alltoolsdetail"
jsfile = "commonhookfetchstream.js"
function1 = 'input.endsWith("assistant/stream")'
function2 = """const chunk = JSON.parse(line.substring(6));
thistext = chunk.parts[0].content[0].text"""
def langmap(self):
return {
"zh": "Simplified Chinese",
"ja": "Japanese",
"en": "English",
"ru": "Russian",
"es": "Spanish",
"ko": "Korean",
"fr": "French",
"cht": "Traditional Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"pl": "Polish",
"uk": "Ukrainian",
"it": "Italian",
"ar": "Arabic",
"th": "Thai",
}
def injectjs(self):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
print(self.Runtime_evaluate("window.injectedjs")["result"]["type"])
return
with open(
os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"),
"r",
encoding="utf8",
) as ff:
js = ff.read() % (
'input.endsWith("assistant/stream")',
"""const chunk = JSON.parse(line.substring(6));
thistext = chunk.parts[0].content[0].text""",
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
def translate(self, content):
self.injectjs()
self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""')
if self.config["use_custom_prompt"]:
prompt = self.config["custom_prompt"]
else:
prompt = "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
)
content = prompt + content
def dotranslate(self, content):
self.Runtime_evaluate(
'document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.input-box-inner > textarea").focus()'
)
self.send_keys(content)
self.Runtime_evaluate("""document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.enter.m-three-row > img").click()""")
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]
self.Runtime_evaluate(
"""document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.enter.m-three-row > img").click()"""
)

View File

@ -1,57 +1,14 @@
from translator.basetranslator_dev import basetransdev
import time, os
from translator.dev_llm_common import commonllmdev
class TS(basetransdev):
class TS(commonllmdev):
target_url = "https://chatgpt.com/"
jsfile = "commonhookfetchstream.js"
function1 = 'input.includes("conversation")'
function2 = r"""const chunk = JSON.parse(line.substring(6));
thistext = chunk.message.content.parts[0]"""
def langmap(self):
return {
"zh": "Simplified Chinese",
"ja": "Japanese",
"en": "English",
"ru": "Russian",
"es": "Spanish",
"ko": "Korean",
"fr": "French",
"cht": "Traditional Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"pl": "Polish",
"uk": "Ukrainian",
"it": "Italian",
"ar": "Arabic",
"th": "Thai",
}
def injectjs(self):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
return
with open(
os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"),
"r",
encoding="utf8",
) as ff:
js = ff.read() % (
'input.includes("conversation")',
r"""const chunk = JSON.parse(line.substring(6));
thistext = chunk.message.content.parts[0]""",
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
def translate(self, content):
self.injectjs()
self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""')
if self.config["use_custom_prompt"]:
prompt = self.config["custom_prompt"]
else:
prompt = "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
)
content = prompt + content
def dotranslate(self, content):
self.Runtime_evaluate(
'textarea=document.querySelector("#prompt-textarea");textarea.value="";event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);textarea.value=`{}`;event = new Event("input", {{bubbles: true, cancelable: true }});textarea.dispatchEvent(event);'.format(
content
@ -60,18 +17,3 @@ class TS(basetransdev):
self.Runtime_evaluate(
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()"""
)
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]

View File

@ -1,73 +1,15 @@
from translator.basetranslator_dev import basetransdev
import time, os
from translator.dev_llm_common import commonllmdev
class TS(basetransdev):
class TS(commonllmdev):
target_url = "https://chat.deepseek.com/"
def langmap(self):
return {
"zh": "Simplified Chinese",
"ja": "Japanese",
"en": "English",
"ru": "Russian",
"es": "Spanish",
"ko": "Korean",
"fr": "French",
"cht": "Traditional Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"pl": "Polish",
"uk": "Ukrainian",
"it": "Italian",
"ar": "Arabic",
"th": "Thai",
}
def injectjs(self):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
return
with open(
os.path.join(os.path.dirname(__file__), "commonhookxhrstream.js"),
"r",
encoding="utf8",
) as ff:
js = ff.read() % (
"input.endsWith('v0/chat/completions')",
r"""const chunk = JSON.parse(line.substring(6));
jsfile = "commonhookxhrstream.js"
function1 = "input.endsWith('v0/chat/completions')"
function2 = r"""const chunk = JSON.parse(line.substring(6));
if(!!(chunk.choices[0].delta.content))
thistext += chunk.choices[0].delta.content""",
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
thistext += chunk.choices[0].delta.content"""
def translate(self, content):
self.injectjs()
self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""')
if self.config["use_custom_prompt"]:
prompt = self.config["custom_prompt"]
else:
prompt = "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
)
content = prompt + content
def dotranslate(self, content):
self.Runtime_evaluate('document.querySelector("#chat-input").click()')
self.send_keys(content)
self.Runtime_evaluate("document.getElementsByClassName('_89d4d19')[1].click()")
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]

View File

@ -0,0 +1,46 @@
from translator.basetranslator_dev import basetransdev
import time, os
class commonllmdev(basetransdev):
def injectjs(self):
with open(
os.path.join(os.path.dirname(__file__), self.jsfile),
"r",
encoding="utf8",
) as ff:
js = ff.read() % (
self.function1,
self.function2,
)
self.Runtime_evaluate(js)
def dotranslate(self, content): ...
def translate(self, content):
self.injectjs()
if self.config["use_custom_prompt"]:
prompt = self.config["custom_prompt"]
else:
prompt = "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
)
content = prompt + content
self.dotranslate(content)
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]

View File

@ -1,77 +1,19 @@
from translator.basetranslator_dev import basetransdev
import time, os
from translator.dev_llm_common import commonllmdev
class TS(basetransdev):
class TS(commonllmdev):
target_url = "https://kimi.moonshot.cn/"
def langmap(self):
return {
"zh": "Simplified Chinese",
"ja": "Japanese",
"en": "English",
"ru": "Russian",
"es": "Spanish",
"ko": "Korean",
"fr": "French",
"cht": "Traditional Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"pl": "Polish",
"uk": "Ukrainian",
"it": "Italian",
"ar": "Arabic",
"th": "Thai",
}
def injectjs(self):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
return
with open(
os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"),
"r",
encoding="utf8",
) as ff:
js = ff.read() % (
'input.endsWith("completion/stream")',
"""const chunk = JSON.parse(line.substring(6));
jsfile = "commonhookfetchstream.js"
function1 = 'input.endsWith("completion/stream")'
function2 = """const chunk = JSON.parse(line.substring(6));
if(chunk.event!='cmpl')continue;
if(chunk.text)
thistext += chunk.text""",
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
thistext += chunk.text"""
def translate(self, content):
self.injectjs()
self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""')
if self.config["use_custom_prompt"]:
prompt = self.config["custom_prompt"]
else:
prompt = "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
)
content = prompt + content
def dotranslate(self, content):
self.Runtime_evaluate(
'document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div.MuiBox-root.css-8atqhb > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div")? document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div.MuiBox-root.css-8atqhb > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div").click() : document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div > div.chatPageLayoutRightBoxLeft___taL5l > div.content___inD6V > div > div.chatBottom___jS9Jd.MuiBox-root.css-jdjpte > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div.editorContentEditable___FZJd9").click()'
)
self.send_keys(content)
self.wait_for_result('document.querySelector("#send-button").disabled', True)
self.Runtime_evaluate("""document.querySelector("#send-button").click()""")
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]

View File

@ -1,74 +1,16 @@
from translator.basetranslator_dev import basetransdev
import time, os
from translator.dev_llm_common import commonllmdev
class TS(basetransdev):
class TS(commonllmdev):
target_url = "https://tongyi.aliyun.com/qianwen"
jsfile = "commonhookfetchstream.js"
function1 = 'input.endsWith("dialog/conversation")'
function2 = r"""const chunk = JSON.parse(line.substring(6));
thistext = chunk.contents[0].content"""
def langmap(self):
return {
"zh": "Simplified Chinese",
"ja": "Japanese",
"en": "English",
"ru": "Russian",
"es": "Spanish",
"ko": "Korean",
"fr": "French",
"cht": "Traditional Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"pl": "Polish",
"uk": "Ukrainian",
"it": "Italian",
"ar": "Arabic",
"th": "Thai",
}
def injectjs(self):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
return
with open(
os.path.join(os.path.dirname(__file__), "commonhookfetchstream.js"),
"r",
encoding="utf8",
) as ff:
js = ff.read() % (
'input.endsWith("dialog/conversation")',
r"""const chunk = JSON.parse(line.substring(6));
thistext = chunk.contents[0].content""",
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
def translate(self, content):
self.injectjs()
self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""')
if self.config["use_custom_prompt"]:
prompt = self.config["custom_prompt"]
else:
prompt = "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
)
content = prompt + content
def dotranslate(self, content):
self.Runtime_evaluate('document.getElementsByTagName("textarea")[0].focus()')
self.send_keys(content)
self.Runtime_evaluate(
r"""document.querySelector("#tongyiPageLayout > div.sc-fQpRED.jsoEZg > div > div.sc-hNGPaV.erDcgy.pageContentWrap--AovzQ5wq > div > div.inputField--PE5FhWzd > div > div.chatInput--eJzBH8LP > div.operateBtn--zFx6rSR0").click()"""
)
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]