Skip to content

Commit

Permalink
feat: add Lingocloud trs
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Aug 30, 2020
1 parent d36e381 commit fe75c7a
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .neutrinorc.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ module.exports = {
from: '+(antd|antd.dark).min.css',
to: 'assets/',
toType: 'dir'
},
// caiyunapp
{
context: 'node_modules/trsjs/build/sala',
from: 'trs.js',
to: 'assets/',
toType: 'dir'
}
]
}),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
"storybook-addon-jsx": "7.1.15",
"storybook-addon-react-docgen": "1.2.32",
"to-string-loader": "^1.1.5",
"trsjs": "caiyunapp/trs.js",
"typescript": "^3.8.2",
"webpack": "^4",
"webpack-bundle-analyzer": "^3.5.2",
Expand Down
3 changes: 2 additions & 1 deletion src/_locales/en/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const locale: typeof _locale = {
baidu_search: 'Baidu Search',
bing_dict: 'Bing Dict',
bing_search: 'Bing Search',
caiyuntrs: 'Lingocloud Page Translate',
cambridge: 'Cambridge',
copy_pdf_url: 'Copy PDF URL to Clipboard',
dictcn: 'Dictcn',
Expand All @@ -24,7 +25,7 @@ export const locale: typeof _locale = {
microsoft_page_translate: 'Microsoft Page Translate',
notebook_title: 'New Word List',
notification_youdao_err:
'Youdao Page Translation 2.0 not responding.\nSaladict might not have permission to access this page.\nIgnore this message if Youdao panal is shown.',
'Youdao Page Translate 2.0 not responding.\nSaladict might not have permission to access this page.\nIgnore this message if Youdao panal is shown.',
oxford: 'Oxford',
page_translations: 'Page Translations',
saladict: 'Saladict',
Expand Down
1 change: 1 addition & 0 deletions src/_locales/zh-CN/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const locale = {
baidu_search: '百度搜索',
bing_dict: '必应词典',
bing_search: '必应搜索',
caiyuntrs: '彩云小译网页翻译',
cambridge: '剑桥词典',
copy_pdf_url: '复制PDF链接到剪贴板',
dictcn: '海词词典',
Expand Down
1 change: 1 addition & 0 deletions src/_locales/zh-TW/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const locale: typeof _locale = {
baidu_search: '百度搜尋',
bing_dict: 'Bing 字典',
bing_search: 'Bing 搜尋',
caiyuntrs: '彩雲小譯網頁翻譯',
cambridge: '劍橋字典',
copy_pdf_url: '複製PDF連結到剪貼簿',
dictcn: '海詞字典',
Expand Down
1 change: 1 addition & 0 deletions src/app-config/context-menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function getAllContextMenus(): { [id: string]: ContextItem } {
baidu_search: 'https://www.baidu.com/s?ie=utf-8&wd=%s',
bing_dict: 'https://cn.bing.com/dict/?q=%s',
bing_search: 'https://www.bing.com/search?q=%s',
caiyuntrs: 'x',
cambridge:
'http://dictionary.cambridge.org/spellcheck/english-chinese-simplified/?q=%s',
copy_pdf_url: 'x',
Expand Down
9 changes: 2 additions & 7 deletions src/app-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default getDefaultConfig

function _getDefaultConfig() {
return {
version: 13,
version: 14,

/** activate app, won't affect triple-ctrl setting */
active: true,
Expand Down Expand Up @@ -321,12 +321,7 @@ function _getDefaultConfig() {
] as [string, string][],

contextMenus: {
selected: [
'view_as_pdf',
'google_translate',
'google_search',
'saladict'
],
selected: ['view_as_pdf', 'caiyuntrs', 'google_translate', 'saladict'],
all: getAllContextMenus()
},

Expand Down
23 changes: 18 additions & 5 deletions src/app-config/merge-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function mergeConfig(
? JSON.parse(JSON.stringify(baseConfig))
: getDefaultConfig()

// pre-merge patch start
/* ----------------------------------------------- *\
Pre-merge Patch Start
\* ----------------------------------------------- */
let oldVersion = oldConfig.version

if (oldVersion < 13) {
Expand All @@ -42,8 +44,9 @@ export function mergeConfig(
rename('tripleCtrlHeight', 'qssaHeight')
rename('tripleCtrlSidebar', 'qssaSidebar')
rename('tripleCtrlPageSel', 'qssaPageSel')

// pre-merge patch end
/* ----------------------------------------------- *\
Pre-merge Patch End
\* ----------------------------------------------- */

Object.keys(base).forEach(key => {
switch (key) {
Expand Down Expand Up @@ -168,7 +171,9 @@ export function mergeConfig(
}
})

// post-merge patch start
/* ----------------------------------------------- *\
Post-merge Patch Start
\* ----------------------------------------------- */
oldVersion = oldConfig.version

if (oldVersion <= 10) {
Expand All @@ -182,6 +187,12 @@ export function mergeConfig(
'https://stackedit.io/*'
])
}
if (oldVersion <= 13) {
oldVersion = 14
if (!base.contextMenus.selected.includes('caiyuntrs')) {
base.contextMenus.selected.unshift('caiyuntrs')
}
}

if (oldConfig.language['minor'] === false) {
base.language.japanese = false
Expand All @@ -194,7 +205,9 @@ export function mergeConfig(
if (base.panelMaxHeightRatio < 1) {
base.panelMaxHeightRatio = Math.round(base.panelMaxHeightRatio * 100)
}
// post-merge patch end
/* ----------------------------------------------- *\
Post-merge Patch End
\* ----------------------------------------------- */

return base

Expand Down
5 changes: 4 additions & 1 deletion src/app-config/merge-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,15 @@ export function mergeProfile(
}

/* ----------------------------------------------- *\
Patch
Patch Start
\* ----------------------------------------------- */
// hjdict changed korean location
if ((base.dicts.all.hjdict.options.chsas as string) === 'kor') {
base.dicts.all.hjdict.options.chsas = 'kr'
}
/* ----------------------------------------------- *\
Patch End
\* ----------------------------------------------- */

return base

Expand Down
9 changes: 9 additions & 0 deletions src/background/context-menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export class ContextMenus {
// })
}

static openCaiyunTrs() {
if (isFirefox) return
browser.tabs.executeScript({ file: '/assets/trs.js' })
}

static openYoudao() {
// FF policy
if (isFirefox) return
Expand Down Expand Up @@ -158,6 +163,9 @@ export class ContextMenus {
case 'google_page_translate':
ContextMenus.openGoogle()
break
case 'caiyuntrs':
ContextMenus.openCaiyunTrs()
break
case 'google_cn_page_translate':
ContextMenus.openGoogle()
break
Expand Down Expand Up @@ -259,6 +267,7 @@ export class ContextMenus {

let contexts: browser.contextMenus.ContextType[]
switch (id) {
case 'caiyuntrs':
case 'google_page_translate':
case 'google_cn_page_translate':
case 'youdao_page_translate':
Expand Down
3 changes: 3 additions & 0 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { init as initPdf } from './pdf-sniffer'
import { ContextMenus } from './context-menus'
import { BackgroundServer } from './server'
import { initBadge } from './badge'
import { setupCaiyunTrsBackend } from './page-translate/caiyun'
import './types'

// init first to recevice self messaging
Expand All @@ -21,6 +22,8 @@ startSyncServiceInterval()
ContextMenus.init()
BackgroundServer.init()

setupCaiyunTrsBackend()

getConfig().then(async config => {
window.appConfig = config
initPdf(config)
Expand Down
34 changes: 34 additions & 0 deletions src/background/page-translate/caiyun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export function setupCaiyunTrsBackend() {
browser.runtime.onMessage.addListener(msg => {
if (msg.contentScriptQuery === 'fetchUrl') {
const requestInit: RequestInit = {
method: msg.method || 'GET',
credentials: 'include',
headers: {
...(msg.headers || {}),
'content-type': 'application/json'
}
}

if (msg.data) {
try {
requestInit.body = JSON.stringify(msg.data)
} catch (error) {
if (process.env.DEBUG) {
console.error('Caiyun trs message data error:', error)
}
}
}

return fetch(msg.url, requestInit)
.then(response => response.text())
.then(text => ({ status: 'ok', data: text }))
.catch(error => {
if (process.env.DEBUG) {
console.error('Caiyun trs requestAuthURL error:', error)
}
return { status: 'error', error: error }
})
}
})
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14771,6 +14771,10 @@ trim@0.0.1:
resolved "https://registry.yarnpkg.com/trim/-/trim-0.0.1.tgz#5858547f6b290757ee95cccc666fb50084c460dd"
integrity sha1-WFhUf2spB1fulczMZm+1AITEYN0=

trsjs@caiyunapp/trs.js:
version "0.1.0"
resolved "https://codeload.github.com/caiyunapp/trs.js/tar.gz/3dd6611fd49b7daab6f62326e4b15710da7d3486"

"true-case-path@^1.0.2":
version "1.0.3"
resolved "https://registry.yarnpkg.com/true-case-path/-/true-case-path-1.0.3.tgz#f813b5a8c86b40da59606722b144e3225799f47d"
Expand Down

0 comments on commit fe75c7a

Please sign in to comment.