Skip to content

Commit

Permalink
fix(sougou): add cookie for logtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
waynecz committed Oct 9, 2019
1 parent 4a11f56 commit 9436636
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 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.9",
"version": "1.2.10",
"description": "translate and remember",
"scripts": {
"dev": "node_modules/.bin/webpack --progress --config build/webpack.dev.js",
Expand Down
12 changes: 8 additions & 4 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { google, sougou, shanbay, cdn, youdao } from './client'
import axios from 'axios'
import { _sougouUuid } from '@/utils'
import md5 from 'md5'

window.seccode = '8511813095151'
window.seccode = 8511813095152

function _escape(text) {
const ele = document.createElement('div')
Expand All @@ -12,7 +13,10 @@ function _escape(text) {

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

// eslint-disable-next-line no-eval
eval(tokenInsertScript)
Expand Down Expand Up @@ -48,15 +52,15 @@ export default {
.join('&')

return sougou.post('/reventondc/translate', data).then(async res => {
if (res.translate.errorCode === '10') {
if (res.translate.errorCode !== '0') {
const lastSecode = window.seccode

await getSeccode()

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

Expand Down
3 changes: 2 additions & 1 deletion src/changelog-breif.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"1.2.6": "修复翻译失败的问题",
"1.2.7": "紧急停用插件的翻译功能,请大家等下一个版本修复,抱歉",
"1.2.8": "翻译功能修复",
"1.2.9": "1.添加隐藏右键菜单选项 2.样式问题修复"
"1.2.9": "1.添加隐藏右键菜单选项 2.样式问题修复",
"1.2.10": "翻译功能修复"
}
60 changes: 45 additions & 15 deletions src/chrome/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const moveWord2NextStage = async word => {
const detectGo = () => {
Storage.get(TR_SETTING_IS_DIRECTLY_KEY, false).then(isDirectly => {
chrome.browserAction.setBadgeText({ text: isDirectly ? 'go' : '' })
chrome.browserAction.setBadgeBackgroundColor({ color: isDirectly ? '#ed559d' : '#fff' })
chrome.browserAction.setBadgeBackgroundColor({
color: isDirectly ? '#ed559d' : '#fff'
})
})
}

Expand Down Expand Up @@ -78,7 +80,8 @@ chrome.runtime.onInstalled.addListener(async reason => {
const { version } = require('@/manifest.json')
chrome.notifications.clear('updateInfo')
chrome.notifications.create('updateInfo', {
iconUrl: 'https://raw.githubusercontent.com/waynecz/dadda-translate-crx/master/src/assets/logo.png',
iconUrl:
'https://raw.githubusercontent.com/waynecz/dadda-translate-crx/master/src/assets/logo.png',
type: 'basic',
title: `${version} 更新:`,
message: require('@/changelog-breif.json')[version] || '点击查看更新内容',
Expand Down Expand Up @@ -259,7 +262,9 @@ chrome.notifications.onButtonClicked.addListener(async (notiId, btnId) => {
*/
chrome.notifications.onClicked.addListener(async notiId => {
if (notiId === 'updateInfo') {
chrome.tabs.create({ url: 'https://github.com/waynecz/dadda-translate-crx/releases' })
chrome.tabs.create({
url: 'https://github.com/waynecz/dadda-translate-crx/releases'
})
}

if (_hasTRId(notiId)) {
Expand All @@ -281,12 +286,15 @@ chrome.notifications.onClicked.addListener(async notiId => {
Storage.get(TR_SETTING_HIDE_CONTEXT_MENU_OPTION, false).then(value => {
if (!value) {
chrome.contextMenus.create({
'title': '达达划词翻译',
'contexts': ['selection', 'page'],
'onclick': (info, tab) => {
title: '达达划词翻译',
contexts: ['selection', 'page'],
onclick: (info, tab) => {
if (info.selectionText) {
chrome.tabs.executeScript({
code: 'document.dispatchEvent(new CustomEvent(\'contextMenuClick\', { bubbles: true, detail: { text: "' + info.selectionText + '" } }))'
code:
"document.dispatchEvent(new CustomEvent('contextMenuClick', { bubbles: true, detail: { text: \"" +
info.selectionText +
'" } }))'
})
}
}
Expand All @@ -296,19 +304,41 @@ Storage.get(TR_SETTING_HIDE_CONTEXT_MENU_OPTION, false).then(value => {

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 })
}
const refererIndex = details.requestHeaders.findIndex(({ name }) => {
return name.toLowerCase() === 'referer'
})

const corsModeIndex = details.requestHeaders.findIndex(({ name }) => {
return name.toLowerCase() === 'sec-fetch-mode'
})

const originIndex = details.requestHeaders.findIndex(({ name }) => {
return name.toLowerCase() === 'origin'
})

if (refererIndex === -1) {
details.requestHeaders.push({ name: 'Referer', value: SOUGOU_HOST })
} else {
details.requestHeaders[refererIndex].value = SOUGOU_HOST
}

if (corsModeIndex !== -1) {
details.requestHeaders[corsModeIndex].value = 'no-cors'
}

if (originIndex === -1) {
details.requestHeaders.push({ name: 'Origin', value: SOUGOU_HOST })
} else {
details.requestHeaders[originIndex].value = SOUGOU_HOST
}

return { requestHeaders: details.requestHeaders }
},
{
urls: ['https://fanyi.sogou.com/reventondc/translate', 'https://fanyi.sogou.com/logtrace']
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.9",
"version": "1.2.10",
"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 9436636

Please sign in to comment.