diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py b/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py index 43a17ca0..4c49e214 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator_dev.py @@ -10,13 +10,14 @@ class basetransdev(basetrans): return url.startswith(self.target_url) def Page_navigate(self,url): - self._SendRequest(self.ws,'Page.navigate',{'url':url}) + self._SendRequest('Page.navigate',{'url':url}) self._wait_document_ready() def Runtime_evaluate(self,expression): - return self._SendRequest(self.ws,'Runtime.evaluate',{"expression":expression}) + return self._SendRequest('Runtime.evaluate',{"expression":expression}) def wait_for_result(self,expression,badresult=''): for i in range(10000): - state =self.Runtime_evaluate( expression) + if self.using==False:return + state =self.Runtime_evaluate(expression) try: if state['result']['value']!=badresult: return state['result']['value'] @@ -28,15 +29,18 @@ class basetransdev(basetrans): self._id=1 self._createtarget() super()._private_init() - def _SendRequest(self,ws,method,params): + def _SendRequest(self,method,params,ws=None): + if self.using==False:return self._id+=1 try: + if ws is None:ws=self.ws ws.send(json.dumps({'id':self._id,'method':method,'params':params})) res=ws.recv() except : print_exc() self._createtarget() - return self._SendRequest(self.ws,method,params) + time.sleep(1) + return self._SendRequest(method,params,ws) res=json.loads(res) try: return res['result'] @@ -44,16 +48,18 @@ class basetransdev(basetrans): print(res) if res['method']=='Inspector.detached' and res['params']['reason']=='target_closed': self._createtarget() - return self._SendRequest(self.ws,method,params) + return self._SendRequest(method,params,ws) def _createtarget(self): + if self.using==False:return port=globalconfig['debugport'] url=self.target_url try: infos=requests.get('http://127.0.0.1:{}/json/list'.format(port)).json() except : print_exc() + time.sleep(1) self._createtarget() return use=None @@ -63,7 +69,7 @@ class basetransdev(basetrans): break if use is None: ws=websocket.create_connection(infos[0]['webSocketDebuggerUrl']) - a=self._SendRequest(ws,'Target.createTarget',{'url':url}) + a=self._SendRequest('Target.createTarget',{'url':url},ws=ws) use= 'ws://127.0.0.1:{}/devtools/page/'.format(port)+a['targetId'] self.ws=websocket.create_connection(use) @@ -71,6 +77,7 @@ class basetransdev(basetrans): def _wait_document_ready(self): for i in range(10000): + if self.using==False:return state =self.Runtime_evaluate( "document.readyState") try: if state['result']['value']=='complete': @@ -78,4 +85,15 @@ class basetransdev(basetrans): except: pass time.sleep(0.1) - \ No newline at end of file + def send_keys(self,text): + self._SendRequest('Input.setIgnoreInputEvents', {'ignore': False}) + try: + self._SendRequest('Input.insertText', {'text': text}) + except: + for char in text: + #self._SendRequest('Input.dispatchKeyEvent', {'type': 'keyDown', 'modifiers': 0, 'timestamp': 0, 'text': char, 'unmodifiedText': char, 'keyIdentifier': '', 'code': f'Key{char.upper()}', 'key': char, 'windowsVirtualKeyCode': code, 'nativeVirtualKeyCode': code, 'autoRepeat': False, 'isKeypad': False, 'isSystemKey': False, 'location': 0}) + self._SendRequest('Input.dispatchKeyEvent', {'type': 'char', 'modifiers': 0, 'timestamp': 0, 'text': char, 'unmodifiedText': char, 'keyIdentifier': '', 'code': 'Unidentified', 'key': '', 'windowsVirtualKeyCode': 0, 'nativeVirtualKeyCode': 0, 'autoRepeat': False, 'isKeypad': False, 'isSystemKey': False, 'location': 0}) + #self._SendRequest('Input.dispatchKeyEvent', {'type': 'keyUp', 'modifiers': 0, 'timestamp': 0, 'text': '', 'unmodifiedText': '', 'keyIdentifier': '', 'code': f'Key{char.upper()}', 'key': char, 'windowsVirtualKeyCode': code, 'nativeVirtualKeyCode': code, 'autoRepeat': False, 'isKeypad': False, 'isSystemKey': False, 'location': 0}) + + self._SendRequest('Input.setIgnoreInputEvents', {'ignore': True}) + \ No newline at end of file diff --git a/LunaTranslator/LunaTranslator/translator/dev_baidu_ai.py b/LunaTranslator/LunaTranslator/translator/dev_baidu_ai.py new file mode 100644 index 00000000..74eb2582 --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/dev_baidu_ai.py @@ -0,0 +1,13 @@ + +from translator.basetranslator_dev import basetransdev +from urllib.parse import quote +import time +class TS(basetransdev): + target_url= 'https://fanyi.baidu.com/mtpe-individual/multimodal#/' + def langmap(self): + return {"es":"spa","ko":"kor","fr":"fra","ja":"jp","cht":"cht","vi":"vie","uk":"ukr"} + def translate(self,content): + self.Runtime_evaluate('''document.querySelector("#editor-text > div.AZLVLJHb > div.Ssl84aLh > span").click()''') + self.Runtime_evaluate('''document.querySelector("#editor-text > div.AZLVLJHb > div.Ssl84aLh > div > div > div").click()''') + self.send_keys(content) + return(self.wait_for_result('''document.querySelector("#trans-selection").innerText''')) \ No newline at end of file diff --git a/LunaTranslator/LunaTranslator/translator/dev_poe.py b/LunaTranslator/LunaTranslator/translator/dev_poe.py new file mode 100644 index 00000000..c66ede8d --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/dev_poe.py @@ -0,0 +1,35 @@ + + +from urllib.parse import quote +from translator.basetranslator_dev import basetransdev +import time + +class TS(basetransdev): + target_url='https://poe.com/' + def inittranslator(self): + list(self.translate('Please help me translate the following text into: '+self.tgtlang)) + + def translate(self,content): + try: + self.num=self.Runtime_evaluate('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.InfiniteScroll_container__PHsd4.ChatMessagesView_infiniteScroll__vk3VX").children.length''')['result']['value'] + except: + self.num=2 + print(self.num) + self.Runtime_evaluate('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.ChatHomeMain_container__l4uRf > div.ChatHomeMain_inputContainer__9mgRh > div > div > textarea").click()''') + self.Runtime_evaluate('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > footer > div > div > div > textarea")''') + self.send_keys(content) + self.Runtime_evaluate('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.ChatHomeMain_container__l4uRf > div.ChatHomeMain_inputContainer__9mgRh > div > button.Button_buttonBase__Bv9Vx.Button_primary__6UIn0.ChatMessageSendButton_sendButton__4ZyI4.ChatMessageInputContainer_sendButton__dBjTt").click()''') + self.Runtime_evaluate('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > footer > div > div > button.Button_buttonBase__Bv9Vx.Button_primary__6UIn0.ChatMessageSendButton_sendButton__4ZyI4.ChatMessageInputContainer_sendButton__dBjTt").click()''') + if 0: + self.wait_for_result('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.InfiniteScroll_container__PHsd4.ChatMessagesView_infiniteScroll__vk3VX > div:nth-child({}) > section.ChatMessageActionBar_actionBar__gyeEs").innerHTML'''.format(self.num+1)) + yield self.wait_for_result('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.InfiniteScroll_container__PHsd4.ChatMessagesView_infiniteScroll__vk3VX > div:nth-child({}) > div:nth-child(2) > div.ChatMessage_messageRow__DHlnq").textContent'''.format(self.num+1)) + currtext='' + while True: + newcurr=self.wait_for_result('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.InfiniteScroll_container__PHsd4.ChatMessagesView_infiniteScroll__vk3VX > div:nth-child({}) > div:nth-child(2) > div.ChatMessage_messageRow__DHlnq").textContent'''.format(self.num+1)) + yield newcurr[len(currtext):] + currtext=newcurr + time.sleep(0.3) + if (self.Runtime_evaluate('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.InfiniteScroll_container__PHsd4.ChatMessagesView_infiniteScroll__vk3VX > div:nth-child({}) > section.ChatMessageActionBar_actionBar__gyeEs")'''.format(self.num+1)))['result']['subtype']=='node': + newcurr=self.wait_for_result('''document.querySelector("#__next > div > div.AnnouncementWrapper_container__Z51yh > div.SidebarLayout_layoutWrapper__mPYi4.SidebarLayout_layoutWrapperOverflow__0hyZ4 > main > div > div > div > div.InfiniteScroll_container__PHsd4.ChatMessagesView_infiniteScroll__vk3VX > div:nth-child({}) > div:nth-child(2) > div.ChatMessage_messageRow__DHlnq").textContent'''.format(self.num+1)) + yield newcurr[len(currtext):] + break \ No newline at end of file diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index 65d644cb..cd2319d6 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -867,6 +867,18 @@ "type": "dev", "name": "百度" }, + "dev_baidu_ai": { + "use": false, + "color": "blue", + "type": "dev", + "name": "百度_ai" + }, + "dev_poe": { + "use": false, + "color": "blue", + "type": "dev", + "name": "poe" + }, "google_dev": { "use": false, "color": "blue",