From 2dfcd639b2144b9d0193dc895d9a0eaaba0f6753 Mon Sep 17 00:00:00 2001 From: waynecz <451578533@qq.com> Date: Fri, 6 Sep 2019 11:29:47 +1000 Subject: [PATCH] fix(sougou): wrong condition lead to infitity request --- package.json | 2 +- src/api/index.js | 43 +++++++++++++++++++---------- src/changelog-breif.json | 4 ++- src/chrome/background/background.js | 21 +++++++++++++- src/manifest.json | 2 +- 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 59eed30..aba93ed 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/api/index.js b/src/api/index.js index 2b2dd81..760d173 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -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, @@ -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 }) }, diff --git a/src/changelog-breif.json b/src/changelog-breif.json index ae39089..6ae96f5 100644 --- a/src/changelog-breif.json +++ b/src/changelog-breif.json @@ -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": "翻译功能修复" } diff --git a/src/chrome/background/background.js b/src/chrome/background/background.js index 488845f..b9a0256 100644 --- a/src/chrome/background/background.js +++ b/src/chrome/background/background.js @@ -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, @@ -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'] +) diff --git a/src/manifest.json b/src/manifest.json index 8b696fc..ba75c36 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -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'",