raycast / extensions

Everything you need to extend Raycast.
https://developers.raycast.com
MIT License
5.32k stars 3.01k forks source link

[Easy Dictionary] Something went wrong when search word #2381

Closed passos closed 2 years ago

passos commented 2 years ago

Extension – Easy Dictionary

Author: @tisfeng

Description

Easy Dictionary went wrong when search wrod

Error:

TypeError: Cannot read properties of undefined (reading 'status')

easydict.js:68:375236

---
65: - Added support for Apple \u{1F34E} system translation.
66: - Fixed deleting input box text will cause repeated query selected text problem.
67: - Fixed the bug of "\u2728 New Version Released" flashing when rendering action button.
68: `;var Ps="EasydictVersionInfoKey",Et="https://github.com",r2="https://api.github.com",Ie=class{constructor(){this.version="1.3.1";this.buildNumber=5;this.versionDate="2022-07-21";this.isNeedPrompt=!1;this.hasPrompted=!1;this.releaseMarkdown=Pi;this.fetchReleaseInfo=async a=>{try{let i=await Os.default.get(a);return console.log(`fetch github cost time: ${i.headers["x-request-cost"]} ms`),Promise.resolve(i.data)}catch(i){return Promise.reject(i)}}}getRepoUrl(){return`${Et}/${Ie.author}/${Ie.repo}`}getReadmeUrl(){return`${Et}/${Ie.author}/${Ie.repo}/#readme`}getIssueUrl(){return`${Et}/${Ie.author}/${Ie.repo}/issues`}getCurrentReleaseTagUrl(){return`${this.getRepoUrl()}/releases/tag/${this.version}`}getChineseWikiUrl(){return`${this.getRepoUrl()}/wiki`}getReleaseApiUrl(){return`${r2}/repos/${Ie.author}/${Ie.repo}/releases/tags/${this.version}`}storeCurrentVersionInfo(){let a=JSON.stringify(this),i=`${Ps}-${this.version}`;return Nt.LocalStorage.setItem(i,a)}.......`
69: `),trans_type:r,detect:t==="auto"},m={headers:{"content-type":"application/json","x-authorization":"token "+qr}};return new Promise((d,l)=>{ba.default.post(n,s,m).then(u=>{let c=u.data;console.log(`caiyun translate: ${c.target}, cost: ${u.headers[fi]} ms`),d({type:"Caiyun",result:c})}).catch(u=>{let c={type:"Caiyun",code:u.response.status.toString(),message:u.response.statusText};l(c),console.error("caiyun error response: ",u.response)})})}async function N0(e,a,i){let n=Q(a).deepLSourceLanguageId,t=Q(i).deepLSourceLanguageId||Q(i).deepLTargetLanguageId;if(!n||!t)return console.warn(`DeepL translate not support language: ${a} --> ${i}`),Promise.resolve({type:"DeepL",result:null});let o=Mt.endsWith(":fx")?"https://api-free.deepl.com/v2/translate":"https://api.deepl.com/v2/translate",r={auth_key:Mt,text:e,source_lang:n,target_lang:t};try{let p=await ba.default.post(o,Po.default.stringify(r)),s=p.data,m=s.translations[0].text;return console.log(`DeepL trans.......`
70:         tell application "Shortcuts Events"
71:           run the shortcut named "${e}" with input "${i}"
---

Steps To Reproduce

  1. install the extension
  2. search word in easy dictionary

The current behavior

report "Something went wrong"

The expected behavior

show the word explaination

Raycast version

Version: 1.38.2

tisfeng commented 2 years ago

Thank you for your feedback, I will check it later.

tisfeng commented 2 years ago

You can try using the latest version to see if the problem still exists.

https://github.com/tisfeng/Raycast-Easydict/releases/tag/1.4.0

tisfeng commented 2 years ago

Looking at the logs I've got some idea, but I can't reproduce the problem on my end, can you describe in detail the steps to reproduce it? For example, what is the input text and does this problem occur every time or only in special cases? @passos

stale[bot] commented 2 years ago

This issue/pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in the next 7 days to keep our backlog clean. Thanks for your contributions.

tisfeng commented 2 years ago

This bug should be fixed in the latest version, please test and verify it and let me know the result. Thanks

passos commented 2 years ago

@tisfeng I have updated the extension to the latest version and the crash is still there.

I tried to clone your code and debug in dev mode. It turns out the problem is the extension is trying to call Youdao's API when it's been launched. However, the domain youdao.com is considered insecurely in my network environment and been blocked. So the call to this API will fail every time the extension been launched. And it will crash at background because the Promise error is not caught.

The current implementation will do this no matter Youdao translation service is disabled in preference or not. I disabled the call getYoudaoWebCookie(); at https://github.com/raycast/extensions/blob/main/extensions/easydict/src/dictionary/youdao/youdao.ts#L41 then it works now.

There is another issue related to this one. I noticed that when query the word, even I only has Google translation enabled and all the other translation services and options disabled (includes "Detect Language Speed First") in extension preference, but it still sends query requests to https://fanyi.baidu.com, https://www.bing.com and https://translate.google.cn.

I think it should follow the preference and don't send any request to the translation service if that translation service is disabled. Otherwise, it might cause the same issue like this one.

tisfeng commented 2 years ago

OK, I know this problem, thanks for your patient feedback.

First of all, I really didn't think about the possible error of getting the Youdao web cookie, which is too strange in your case. And getYoudaoWebCookie() this method is called every time the extension is started, to reduce the response time of subsequent Youdao requests. I will fix it later.

Regarding the second point you mentioned, it is actually normal, because the whole translation process is divided into two steps, the first one is to detect the text first, and the second one is to request the corresponding translation API. And in order to accurately detect the input, I have used Tencent, Baidu, Bing and Google for a total of four language detection API, and they will concurrently request once the text is input, which will not slow the speed.

In addition, I have made a special treatment for the language detection module, even if some of them fail to request, under normal circumstances will not cause the extension to crash, just may not be detected so accurately, so don't have to worry about this.

tisfeng commented 2 years ago

The latest version should have fixed the above bug, https://github.com/tisfeng/Raycast-Easydict/commit/0c11ace2667a39b81421c3c04f181267eeeec2f9 , you can test it out. If there is no problem, I will release a fixed version later.

passos commented 2 years ago

Thanks for the fix. It's not crash anymore with this update.

Btw, regard to the language detection feature, I turned off the option "Detect Language Speed First" and also set the "First Language" and "Second Language" it still requests to previous API endpoints (Google, Bing, Baidu, Tencent, Youdao, ...). I would love to have an option that can turn off this feature because I personally don't want to send my data to Tencent, Baidu or Youdao. Of course, this is nothing related to the crash in this issue.

tisfeng commented 2 years ago

The current language detection module will request all language detect APIs concurrently, and the Detect Language Speed First option only means to save time by quickly confirming the language type while maintaining as much accuracy as possible.

I already know your needs, in the future I may add an option to use only the language detection APIs of the services the user has turned on, for example, if only Google and Bing Translate are turned on, only those two language detection APIs will be used. But this may result in inaccurate language detection, as well as slowness. This is planned for future development, low priority.

tisfeng commented 2 years ago

Hi @passos, I've just released a new version with improvements to privacy issues https://github.com/raycast/extensions/pull/3061

Privacy improvement: Except for Google and Bing language detection (no key required, no personal information will be recorded!), other language detection will only be enabled when the corresponding translation engine is turned on by the user.

However, please note that in general the more language detection API there are, the faster the query response will be, and conversely, if only Google and Bing language detection API is used, the speed may be slow.