Skip to content

Commit

Permalink
fix(sougou): wrong condition lead to infitity request
Browse files Browse the repository at this point in the history
  • Loading branch information
waynecz committed Sep 6, 2019
1 parent f2430b8 commit 2dfcd63
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dadda-translate",
"version": "1.2.6",
"version": "1.2.8",
"description": "translate and remember",
"scripts": {
"dev": "node_modules/.bin/webpack --progress --config build/webpack.dev.js",
Expand Down
43 changes: 29 additions & 14 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@ import { google, sougou, shanbay, cdn, youdao } from './client'
import { _sougouUuid } from '@/utils'
import md5 from 'md5'

window.seccode = 'b33bf8c58706155663d1ad5dba4192dc'
window.seccode = '8511813095151'

function _escape(text) {
const ele = document.createElement('div')
ele.appendChild(document.createTextNode(text))
return ele.innerHTML
}

// 获取 seccode
async function getSeccode() {
const tokenInsertScript = await sougou.get('https://fanyi.sogou.com/logtrace')

// eslint-disable-next-line no-eval
eval(tokenInsertScript)
}

export default {
sougouTranslate(text) {
const from = 'auto'
const to = 'zh-CHS'

const s = md5('' + from + to + text + window.seccode)
text = encodeURIComponent(text).replace(/%20/g, '+')
const textAfterEscape = _escape(text)

const s = md5('' + from + to + textAfterEscape + window.seccode)

const payload = {
from,
to,
client: 'pc',
fr: 'browser_pc',
text,
text: textAfterEscape,
useDetect: 'on',
useDetectResult: 'on',
needQc: 1,
Expand All @@ -33,19 +48,19 @@ export default {
.join('&')

return sougou.post('/reventondc/translate', data).then(async res => {
if (res.errorCode === 0) return res
// 如果翻译失败,尝试从源码中获取token
const tokenInsertScript = await sougou.get('https://fanyi.sogou.com/logtrace')
console.log('TCL: sougouTranslate -> s', tokenInsertScript)
if (res.translate.errorCode === '10') {
const lastSecode = window.seccode

// eslint-disable-next-line no-eval
eval(tokenInsertScript)
await getSeccode()

console.log(window.seccode)

if (!window.seccode) throw res
if (window.seccode === lastSecode) {
throw res
} else {
this.sougouTranslate(text)
}
}

return this.sougouTranslate(text)
return res
})
},

Expand Down
4 changes: 3 additions & 1 deletion src/changelog-breif.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
"1.2.3": "修复部分网站翻译图标不出现的问题",
"1.2.4": "1. 新增右键翻译,可翻译 pdf 2. 修复吐司弹词",
"1.2.5": "新增「只显示英英翻译」开关(推荐打开",
"1.2.6": "修复翻译失败的问题"
"1.2.6": "修复翻译失败的问题",
"1.2.7": "紧急停用插件的翻译功能,请大家等下一个版本修复,抱歉",
"1.2.8": "翻译功能修复"
}
21 changes: 20 additions & 1 deletion src/chrome/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Toast from '@/chrome/toast'
import setNewAlarm from '@/chrome/alarm'

import { _removeTRId, _hasTRId, _wrapTRId, _sleep } from '@/utils'
import { DICTIONARY_HOST } from '@/api/host'
import { DICTIONARY_HOST, SOUGOU_HOST } from '@/api/host'
import {
DELAY_MINS_IN_EVERY_STAGE,
TR_SETTING_HAS_TOAST_KEY,
Expand Down Expand Up @@ -285,3 +285,22 @@ chrome.contextMenus.create({
}
}
})

chrome.webRequest.onBeforeSendHeaders.addListener(
details => {
for (var n in details.requestHeaders) {
const hasReferer = details.requestHeaders[n].name.toLowerCase() === 'referer'
if (hasReferer) {
details.requestHeaders[n].value = SOUGOU_HOST
} else {
details.requestHeaders.push({ name: 'Referer', value: SOUGOU_HOST })
}
}

return { requestHeaders: details.requestHeaders }
},
{
urls: ['https://fanyi.sogou.com/reventondc/translate', 'https://fanyi.sogou.com/logtrace']
},
['requestHeaders', 'blocking', 'extraHeaders']
)
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "划词翻译、生词本、吐司弹词、与扇贝同步",
"name": "达达划词翻译",
"manifest_version": 2,
"version": "1.2.6",
"version": "1.2.8",
"author": "Waynecz <451578533@qq.com>",
"homepage_url": "https://github.com/waynecz/dadda-translate-crx",
"content_security_policy": "font-src 'self' https://fonts.googleapis.com; script-src 'self' 'unsafe-eval'; object-src 'self'",
Expand Down

0 comments on commit 2dfcd63

Please sign in to comment.