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

View File

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

View File

@ -1,15 +1,17 @@
const originXHR = XMLHttpRequest (function () {
var hasdone = true window.hasdone = false
var thistext = '' window.thistext = ''
window.XMLHttpRequest = function () { if (window.injectedjs) return
const originXHR = XMLHttpRequest
window.XMLHttpRequest = function () {
var newxhr = new originXHR() var newxhr = new originXHR()
newxhr.open_ori = newxhr.open newxhr.open_ori = newxhr.open
newxhr.open = function () { newxhr.open = function () {
var input = arguments[1] var input = arguments[1]
if (%s) { if (%s) {
hasdone = false; window.hasdone = false;
thistext = '' window.thistext = ''
newxhr.onprogress_ori = newxhr.onprogress newxhr.onprogress_ori = newxhr.onprogress
newxhr.onload_ori = newxhr.onload_ori newxhr.onload_ori = newxhr.onload_ori
newxhr.offset = 0 newxhr.offset = 0
@ -41,4 +43,5 @@ window.XMLHttpRequest = function () {
}; };
return newxhr return newxhr
} }
})()

View File

@ -1,76 +1,19 @@
from translator.basetranslator_dev import basetransdev from translator.dev_llm_common import commonllmdev
import time, os
class TS(basetransdev): class TS(commonllmdev):
target_url = "https://chatglm.cn/main/alltoolsdetail" 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): def dotranslate(self, content):
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
self.Runtime_evaluate( self.Runtime_evaluate(
'document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.input-box-inner > textarea").focus()' '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.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()""") self.Runtime_evaluate(
if self.config["usingstream"]: """document.querySelector("#search-input-box > div.input-wrap.flex.flex-x-between.flex-y-center > div.enter.m-three-row > img").click()"""
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,57 +1,14 @@
from translator.basetranslator_dev import basetransdev from translator.dev_llm_common import commonllmdev
import time, os
class TS(basetransdev): class TS(commonllmdev):
target_url = "https://chatgpt.com/" 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): def dotranslate(self, content):
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
self.Runtime_evaluate( 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( '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 content
@ -60,18 +17,3 @@ 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 > 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()""" 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 from translator.dev_llm_common import commonllmdev
import time, os
class TS(basetransdev): class TS(commonllmdev):
target_url = "https://chat.deepseek.com/" target_url = "https://chat.deepseek.com/"
jsfile = "commonhookxhrstream.js"
def langmap(self): function1 = "input.endsWith('v0/chat/completions')"
return { function2 = r"""const chunk = JSON.parse(line.substring(6));
"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));
if(!!(chunk.choices[0].delta.content)) if(!!(chunk.choices[0].delta.content))
thistext += chunk.choices[0].delta.content""", thistext += chunk.choices[0].delta.content"""
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
def translate(self, content): def dotranslate(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
self.Runtime_evaluate('document.querySelector("#chat-input").click()') self.Runtime_evaluate('document.querySelector("#chat-input").click()')
self.send_keys(content) self.send_keys(content)
self.Runtime_evaluate("document.getElementsByClassName('_89d4d19')[1].click()") 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 from translator.dev_llm_common import commonllmdev
import time, os
class TS(basetransdev): class TS(commonllmdev):
target_url = "https://kimi.moonshot.cn/" target_url = "https://kimi.moonshot.cn/"
jsfile = "commonhookfetchstream.js"
def langmap(self): function1 = 'input.endsWith("completion/stream")'
return { function2 = """const chunk = JSON.parse(line.substring(6));
"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));
if(chunk.event!='cmpl')continue; if(chunk.event!='cmpl')continue;
if(chunk.text) if(chunk.text)
thistext += chunk.text""", thistext += chunk.text"""
)
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
def translate(self, content): def dotranslate(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
self.Runtime_evaluate( 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()' '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.send_keys(content)
self.wait_for_result('document.querySelector("#send-button").disabled', True) self.wait_for_result('document.querySelector("#send-button").disabled', True)
self.Runtime_evaluate("""document.querySelector("#send-button").click()""") 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 from translator.dev_llm_common import commonllmdev
import time, os
class TS(basetransdev): class TS(commonllmdev):
target_url = "https://tongyi.aliyun.com/qianwen" 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): def dotranslate(self, content):
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
self.Runtime_evaluate('document.getElementsByTagName("textarea")[0].focus()') self.Runtime_evaluate('document.getElementsByTagName("textarea")[0].focus()')
self.send_keys(content) self.send_keys(content)
self.Runtime_evaluate( 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()""" 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"]