Skip to content

Commit

Permalink
fix failed to fetch global config
Browse files Browse the repository at this point in the history
  • Loading branch information
plainheart committed Jan 12, 2023
1 parent b5653bb commit 6858aea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
19 changes: 6 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const lang = require('./lang')

const TRANSLATE_API_ROOT = 'https://{s}bing.com'
const TRANSLATE_WEBSITE = TRANSLATE_API_ROOT + '/translator'
const TRANSLATE_API = TRANSLATE_API_ROOT + '/ttranslatev3'
const TRANSLATE_SPELL_CHECK_API = TRANSLATE_API_ROOT + '/tspellcheckv3'
const TRANSLATE_API = TRANSLATE_API_ROOT + '/ttranslatev3?isVertical=1'
const TRANSLATE_SPELL_CHECK_API = TRANSLATE_API_ROOT + '/tspellcheckv3?isVertical=1'

const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.127 Safari/537.36'

Expand Down Expand Up @@ -48,9 +48,6 @@ async function fetchGlobalConfig(userAgent, proxyAgents) {
let token
let key
let tokenExpiryInterval
let isVertical
let frontDoorBotClassification
let isSignedInOrCorporateUser
let cookie
try {
const { body, headers, request: { redirects } } = await got(replaceSubdomain(TRANSLATE_WEBSITE, subdomain), {
Expand All @@ -69,8 +66,8 @@ async function fetchGlobalConfig(userAgent, proxyAgents) {
IID = body.match(/data-iid="([^"]+)"/)[1]

// required
;[key, token, tokenExpiryInterval, isVertical, frontDoorBotClassification, isSignedInOrCorporateUser] = JSON.parse(
body.match(/params_RichTranslateHelper\s?=\s?([^\]]+\])/)[1]
;[key, token, tokenExpiryInterval] = JSON.parse(
body.match(/params_AbusePreventionHelper\s?=\s?([^\]]+\])/)[1]
)
} catch (e) {
console.error('failed to fetch global config', e)
Expand All @@ -84,21 +81,17 @@ async function fetchGlobalConfig(userAgent, proxyAgents) {
token,
tokenTs: key,
tokenExpiryInterval,
isVertical,
frontDoorBotClassification,
isSignedInOrCorporateUser,
cookie,
// PENDING: reset count if count value is large?
count: 0
}
}

function makeRequestURL(isSpellCheck) {
const { IG, IID, subdomain, isVertical } = globalConfig
const { IG, IID, subdomain } = globalConfig
return replaceSubdomain(isSpellCheck ? TRANSLATE_SPELL_CHECK_API : TRANSLATE_API, subdomain)
+ '?isVertical=' + +isVertical
+ (IG && IG.length ? '&IG=' + IG : '')
+ (IID && IID.length ? '&IID=' + IID + '.' + (globalConfig.count++) : '')
+ (IID && IID.length ? '&IID=' + IID + '.' + (++globalConfig.count) : '')
}

function makeRequestBody(isSpellCheck, text, fromLang, toLang) {
Expand Down
10 changes: 2 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ translate('gradent', null, 'en', true)
.catch(onErr)

// correct short text to `this text is very long`
translate('this text is very lang', null, 'en', true)
translate('this text is vry lang', null, 'en', true)
.then(printCorrectRes)
.catch(onErr)

Expand All @@ -61,12 +61,6 @@ translate('this text is very long this text is very long this text is very long
.catch(onErr)

// max text len -> return `undefined` for exceeding max length
translate((() => {
let text = ''
while (text.length < 1001) {
text += ~~(Math.random() * 10) + ''
}
return text
})(), null, 'en')
translate(new Array(1001).fill('0').join(''), null, 'en')
.then(printRes)
.catch(e => onErr(e, true))

0 comments on commit 6858aea

Please sign in to comment.