Skip to content

Commit

Permalink
Moved config.userLanguage to env.browser.language for improved st…
Browse files Browse the repository at this point in the history
…ructure ↞ [auto-sync from https://github.com/adamlui/ai-web-extensions]
  • Loading branch information
kudo-sync-bot committed Nov 23, 2024
1 parent 888a66d commit c50af66
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions greasemonkey/duckduckgpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
// @description:zu Yengeza izimpendulo ze-AI ku-DuckDuckGo (inikwa amandla yi-GPT-4o!)
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.11.22
// @version 2024.11.22.1
// @license MIT
// @icon https://media.ddgpt.com/images/icons/duckduckgpt/icon48.png?af89302
// @icon64 https://media.ddgpt.com/images/icons/duckduckgpt/icon64.png?af89302
Expand Down Expand Up @@ -245,7 +245,7 @@

// Init ENV info
const env = {
browser: {},
browser: { language: chatgpt.getUserLanguage() },
scriptManager: {
name: (() => { try { return GM_info.scriptHandler } catch (err) { return 'unknown' }})(),
version: (() => { try { return GM_info.version } catch (err) { return 'unknown' }})()
Expand Down Expand Up @@ -341,11 +341,11 @@
// Init CONFIG
log.debug('Initializing config...')
Object.assign(config, { userLanguage: chatgpt.getUserLanguage(), minFontSize: 11, maxFontSize: 24, lineHeightRatio: 1.28 })
config.userLocale = config.userLanguage.includes('-') ? config.userLanguage.split('-')[1].toLowerCase() : ''
config.userLocale = env.browser.language.includes('-') ? env.browser.language.split('-')[1].toLowerCase() : ''
settings.load('anchored', 'autoGet', 'autoFocusChatbarDisabled', 'autoScroll', 'bgAnimationsDisabled', 'expanded',
'fgAnimationsDisabled', 'fontSize', 'minimized', 'notFirstRun', 'prefixEnabled', 'proxyAPIenabled',
'replyLanguage', 'rqDisabled', 'scheme', 'stickySidebar', 'streamingDisabled', 'suffixEnabled', 'widerSidebar')
if (!config.replyLanguage) settings.save('replyLanguage', config.userLanguage) // init reply language if unset
if (!config.replyLanguage) settings.save('replyLanguage', env.browser.language) // init reply language if unset
if (!config.fontSize) settings.save('fontSize', 14) // init reply font size if unset
if (!streamingSupported.byBrowser || !streamingSupported.byScriptManager) settings.save('streamingDisabled', true) // disable Streaming in unspported env
if (!config.notFirstRun && env.browser.isMobile) settings.save('autoGet', true) // reverse default auto-get disabled if mobile
Expand Down Expand Up @@ -517,10 +517,10 @@
state_on: 'On',
state_off: 'Off'
}
if (!config.userLanguage.startsWith('en')) { // localize msgs for non-English users
if (!env.browser.language.startsWith('en')) { // localize msgs for non-English users
const localizedMsgs = await new Promise(resolve => {
const msgHostDir = app.urls.assetHost + '/greasemonkey/_locales/',
msgLocaleDir = ( config.userLanguage ? config.userLanguage.replace('-', '_') : 'en' ) + '/'
msgLocaleDir = ( env.browser.language ? env.browser.language.replace('-', '_') : 'en' ) + '/'
let msgHref = msgHostDir + msgLocaleDir + 'messages.json', msgXHRtries = 0
function fetchMsgs() { xhr({ method: 'GET', url: msgHref, onload: handleMsgs })}
function handleMsgs(resp) {
Expand All @@ -532,7 +532,7 @@
resolve(flatMsgs)
} catch (err) { // if bad response
msgXHRtries++ ; if (msgXHRtries == 3) return resolve({}) // try up to 3X (original/region-stripped/EN) only
msgHref = config.userLanguage.includes('-') && msgXHRtries == 1 ? // if regional lang on 1st try...
msgHref = env.browser.language.includes('-') && msgXHRtries == 1 ? // if regional lang on 1st try...
msgHref.replace(/([^_]+_[^_]+)_[^/]*(\/.*)/, '$1$2') // ...strip region before retrying
: ( msgHostDir + 'en/messages.json' ) // else use default English messages
fetchMsgs()
Expand Down Expand Up @@ -691,7 +691,7 @@
const updateModal = document.getElementById(updateModalID).firstChild

// Localize button labels if needed
if (!config.userLanguage.startsWith('en')) {
if (!env.browser.language.startsWith('en')) {
log.debug('Localizing button labels in non-English alert...')
const updateAlert = document.querySelector(`[id="${updateModalID}"]`),
updateBtns = updateAlert.querySelectorAll('button')
Expand Down Expand Up @@ -1023,7 +1023,7 @@
[2, 3].includes(replyLanguage.length) || replyLanguage.includes('-') ? replyLanguage.toUpperCase()
: replyLanguage.charAt(0).toUpperCase() + replyLanguage.slice(1).toLowerCase() )
log.debug('Saving reply language...')
settings.save('replyLanguage', replyLanguage || config.userLanguage)
settings.save('replyLanguage', replyLanguage || env.browser.language)
log.debug(`Success! config.replyLanguage = ${config.replyLanguage}`)
const langUpdatedAlertID = siteAlert(( app.msgs.alert_langUpdated ) + '!', // title
`${app.name} ${app.msgs.alert_willReplyIn} `
Expand Down

0 comments on commit c50af66

Please sign in to comment.