Skip to content

Commit

Permalink
Moved app.msgs init/localization up for improved structure ↞ [auto-…
Browse files Browse the repository at this point in the history
  • Loading branch information
kudo-sync-bot committed Nov 23, 2024
1 parent 27e62bc commit 5748575
Showing 1 changed file with 144 additions and 144 deletions.
288 changes: 144 additions & 144 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.4
// @version 2024.11.22.5
// @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 @@ -255,6 +255,120 @@
app.urls.assetHost = app.urls.gitHub.replace('github.com', 'cdn.jsdelivr.net/gh') + `@${app.latestAssetCommitHash}`
app.urls.update = app.urls.greasyFork.replace('https://', 'https://update.')
.replace(/(\d+)-?([a-z-]*)$/i, (_, id, name) => `${id}/${ name || 'script' }.meta.js`)
app.msgs = {
appDesc: 'Adds ChatGPT answers to DuckDuckGo sidebar (powered by GPT-4o!)',
menuLabel_proxyAPImode: 'Proxy API Mode',
menuLabel_autoGetAnswers: 'Auto-Get Answers',
menuLabel_autoFocusChatbar: 'Auto-Focus Chatbar',
menuLabel_whenStreaming: 'when streaming',
menuLabel_show: 'Show',
menuLabel_relatedQueries: 'Related Queries',
menuLabel_require: 'Require',
menuLabel_beforeQuery: 'before query',
menuLabel_afterQuery: 'after query',
menuLabel_widerSidebar: 'Wider Sidebar',
menuLabel_stickySidebar: 'Sticky Sidebar',
menuLabel_pinTo: 'Pin to',
menuLabel_top: 'Top',
menuLabel_sidebar: 'Sidebar',
menuLabel_bottom: 'Bottom',
menuLabel_background: 'Background',
menuLabel_foreground: 'Foreground',
menuLabel_animations: 'Animations',
menuLabel_replyLanguage: 'Reply Language',
menuLabel_colorScheme: 'Color Scheme',
menuLabel_auto: 'Auto',
menuLabel_about: 'About',
menuLabel_settings: 'Settings',
about_version: 'Version',
about_poweredBy: 'Powered by',
about_sourceCode: 'Source code',
scheme_light: 'Light',
scheme_dark: 'Dark',
mode_proxy: 'Proxy Mode',
mode_streaming: 'Streaming Mode',
mode_autoScroll: 'Auto-Scroll',
mode_prefix: 'Prefix Mode',
mode_suffix: 'Suffix Mode',
mode_anchor: 'Anchor Mode',
mode_debug: 'Debug Mode',
tooltip_playAnswer: 'Play answer',
tooltip_fontSize: 'Font size',
tooltip_sendReply: 'Send reply',
tooltip_askRandQuestion: 'Ask random question',
tooltip_minimize: 'Minimize',
tooltip_restore: 'Restore',
tooltip_expand: 'Expand',
tooltip_shrink: 'Shrink',
tooltip_close: 'Close',
tooltip_copy: 'Copy',
tooltip_reply: 'Reply',
tooltip_code: 'Code',
tooltip_sendRelatedQuery: 'Send related query',
helptip_proxyAPImode: 'Uses a Proxy API for no-login access to AI',
helptip_streamingMode: 'Receive replies in a continuous text stream',
helptip_autoGetAnswers: 'Auto-send queries to DuckDuckGPT when using search engine',
helptip_autoFocusChatbar: 'Auto-focus chatbar whenever it appears',
helptip_autoScroll: 'Auto-scroll responses as they generate in Streaming Mode',
helptip_showRelatedQueries: 'Show related queries below chatbar',
helptip_prefixMode: 'Require "/" before queries for answers to show',
helptip_suffixMode: 'Require "?" after queries for answers to show',
helptip_widerSidebar: 'Horizontally expand search page sidebar',
helptip_stickySidebar: 'Makes DuckDuckGPT visible in sidebar even as you scroll',
helptip_anchorMode: 'Anchor DuckDuckGPT to bottom of window',
helptip_bgAnimations: 'Show animated backgrounds in UI components',
helptip_fgAnimations: 'Show foreground animations in UI components',
helptip_replyLanguage: 'Language for DuckDuckGPT to reply in',
helptip_colorScheme: 'Scheme to display DuckDuckGPT UI components in',
helptip_debugMode: 'Show detailed logging in browser console',
placeholder_typeSomething: 'Type something',
placeholder_askSomethingElse: 'Ask something else',
prompt_updateReplyLang: 'Update reply language',
alert_langUpdated: 'Language updated',
alert_willReplyIn: 'will reply in',
alert_yourSysLang: 'your system language',
alert_choosePlatform: 'Choose a platform',
alert_updateAvail: 'Update available',
alert_newerVer: 'An update to',
alert_isAvail: 'is available',
alert_upToDate: 'Up-to-date',
alert_isUpToDate: 'is up-to-date',
alert_isUnsupportedIn: 'is unsupported in',
alert_whenUsing: 'when using',
alert_pleaseUse: 'Please use',
alert_instead: 'instead',
alert_unavailable: 'unavailable',
alert_isOnlyAvailFor: 'is only available for',
alert_and: 'and',
alert_userscriptMgrNoStream: 'Your userscript manager does not support returning stream responses',
alert_isCurrentlyOnlyAvailBy: 'is currently only available by',
alert_openAIsupportSoon: 'Support for OpenAI API will be added shortly',
alert_waitingFor: 'Waiting for',
alert_response: 'response',
alert_login: 'Please login',
alert_thenRefreshPage: 'then refresh this page',
alert_tooManyRequests: 'ChatGPT is flooded with too many requests',
alert_parseFailed: 'Failed to parse response JSON',
alert_checkCloudflare: 'Please pass Cloudflare security check',
alert_notWorking: 'is not working',
alert_ifIssuePersists: 'If issue persists',
alert_try: 'Try',
alert_switchingOn: 'switching on',
alert_switchingOff: 'switching off',
notif_copiedToClipboard: 'Copied to clipboard',
btnLabel_sendQueryToApp: 'Send search query to DuckDuckGPT',
btnLabel_moreAIextensions: 'More AI Extensions',
btnLabel_rateUs: 'Rate Us',
btnLabel_getSupport: 'Get Support',
btnLabel_updateCheck: 'Check for Updates',
btnLabel_update: 'Update',
btnLabel_dismiss: 'Dismiss',
link_viewChanges: 'View changes',
link_shareFeedback: 'Share Feedback',
prefix_exit: 'Exit',
state_on: 'On',
state_off: 'Off'
}

// Init DEBUG mode
const config = {}, settings = {
Expand Down Expand Up @@ -332,6 +446,35 @@
}
)

// LOCALIZE app.msgs for non-English users
if (!env.browser.language.startsWith('en')) {
log.debug('Localizing app messages...')
const localizedMsgs = await new Promise(resolve => {
const msgHostDir = app.urls.assetHost + '/greasemonkey/_locales/',
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) {
try { // to return localized messages.json
const msgs = JSON.parse(resp.responseText), flatMsgs = {}
for (const key in msgs) // remove need to ref nested keys
if (typeof msgs[key] == 'object' && 'message' in msgs[key])
flatMsgs[key] = msgs[key].message
resolve(flatMsgs)
} catch (err) { // if bad response
msgXHRtries++ ; if (msgXHRtries == 3) return resolve({}) // try up to 3X (original/region-stripped/EN) only
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()
}
}
fetchMsgs()
})
Object.assign(app.msgs, localizedMsgs)
log.debug(`Success! app.msgs = ${log.prettifyObj(app.msgs)}`)
}

// Init COMPATIBILITY flags
log.debug('Initializing compatibility flags...')
const streamingSupported = {
Expand Down Expand Up @@ -401,149 +544,6 @@
inputEvents[action] = ( window.PointerEvent ? 'pointer' : env.browser.isMobile ? 'touch' : 'mouse' ) + action)
log.debug(`Success! inputEvents = ${log.prettifyObj(inputEvents)}`)

// Init app MESSAGES
log.debug('Initializing app messages...')
app.msgs = {
appDesc: 'Adds ChatGPT answers to DuckDuckGo sidebar (powered by GPT-4o!)',
menuLabel_proxyAPImode: 'Proxy API Mode',
menuLabel_autoGetAnswers: 'Auto-Get Answers',
menuLabel_autoFocusChatbar: 'Auto-Focus Chatbar',
menuLabel_whenStreaming: 'when streaming',
menuLabel_show: 'Show',
menuLabel_relatedQueries: 'Related Queries',
menuLabel_require: 'Require',
menuLabel_beforeQuery: 'before query',
menuLabel_afterQuery: 'after query',
menuLabel_widerSidebar: 'Wider Sidebar',
menuLabel_stickySidebar: 'Sticky Sidebar',
menuLabel_pinTo: 'Pin to',
menuLabel_top: 'Top',
menuLabel_sidebar: 'Sidebar',
menuLabel_bottom: 'Bottom',
menuLabel_background: 'Background',
menuLabel_foreground: 'Foreground',
menuLabel_animations: 'Animations',
menuLabel_replyLanguage: 'Reply Language',
menuLabel_colorScheme: 'Color Scheme',
menuLabel_auto: 'Auto',
menuLabel_about: 'About',
menuLabel_settings: 'Settings',
about_version: 'Version',
about_poweredBy: 'Powered by',
about_sourceCode: 'Source code',
scheme_light: 'Light',
scheme_dark: 'Dark',
mode_proxy: 'Proxy Mode',
mode_streaming: 'Streaming Mode',
mode_autoScroll: 'Auto-Scroll',
mode_prefix: 'Prefix Mode',
mode_suffix: 'Suffix Mode',
mode_anchor: 'Anchor Mode',
mode_debug: 'Debug Mode',
tooltip_playAnswer: 'Play answer',
tooltip_fontSize: 'Font size',
tooltip_sendReply: 'Send reply',
tooltip_askRandQuestion: 'Ask random question',
tooltip_minimize: 'Minimize',
tooltip_restore: 'Restore',
tooltip_expand: 'Expand',
tooltip_shrink: 'Shrink',
tooltip_close: 'Close',
tooltip_copy: 'Copy',
tooltip_reply: 'Reply',
tooltip_code: 'Code',
tooltip_sendRelatedQuery: 'Send related query',
helptip_proxyAPImode: 'Uses a Proxy API for no-login access to AI',
helptip_streamingMode: 'Receive replies in a continuous text stream',
helptip_autoGetAnswers: 'Auto-send queries to DuckDuckGPT when using search engine',
helptip_autoFocusChatbar: 'Auto-focus chatbar whenever it appears',
helptip_autoScroll: 'Auto-scroll responses as they generate in Streaming Mode',
helptip_showRelatedQueries: 'Show related queries below chatbar',
helptip_prefixMode: 'Require "/" before queries for answers to show',
helptip_suffixMode: 'Require "?" after queries for answers to show',
helptip_widerSidebar: 'Horizontally expand search page sidebar',
helptip_stickySidebar: 'Makes DuckDuckGPT visible in sidebar even as you scroll',
helptip_anchorMode: 'Anchor DuckDuckGPT to bottom of window',
helptip_bgAnimations: 'Show animated backgrounds in UI components',
helptip_fgAnimations: 'Show foreground animations in UI components',
helptip_replyLanguage: 'Language for DuckDuckGPT to reply in',
helptip_colorScheme: 'Scheme to display DuckDuckGPT UI components in',
helptip_debugMode: 'Show detailed logging in browser console',
placeholder_typeSomething: 'Type something',
placeholder_askSomethingElse: 'Ask something else',
prompt_updateReplyLang: 'Update reply language',
alert_langUpdated: 'Language updated',
alert_willReplyIn: 'will reply in',
alert_yourSysLang: 'your system language',
alert_choosePlatform: 'Choose a platform',
alert_updateAvail: 'Update available',
alert_newerVer: 'An update to',
alert_isAvail: 'is available',
alert_upToDate: 'Up-to-date',
alert_isUpToDate: 'is up-to-date',
alert_isUnsupportedIn: 'is unsupported in',
alert_whenUsing: 'when using',
alert_pleaseUse: 'Please use',
alert_instead: 'instead',
alert_unavailable: 'unavailable',
alert_isOnlyAvailFor: 'is only available for',
alert_and: 'and',
alert_userscriptMgrNoStream: 'Your userscript manager does not support returning stream responses',
alert_isCurrentlyOnlyAvailBy: 'is currently only available by',
alert_openAIsupportSoon: 'Support for OpenAI API will be added shortly',
alert_waitingFor: 'Waiting for',
alert_response: 'response',
alert_login: 'Please login',
alert_thenRefreshPage: 'then refresh this page',
alert_tooManyRequests: 'ChatGPT is flooded with too many requests',
alert_parseFailed: 'Failed to parse response JSON',
alert_checkCloudflare: 'Please pass Cloudflare security check',
alert_notWorking: 'is not working',
alert_ifIssuePersists: 'If issue persists',
alert_try: 'Try',
alert_switchingOn: 'switching on',
alert_switchingOff: 'switching off',
notif_copiedToClipboard: 'Copied to clipboard',
btnLabel_sendQueryToApp: 'Send search query to DuckDuckGPT',
btnLabel_moreAIextensions: 'More AI Extensions',
btnLabel_rateUs: 'Rate Us',
btnLabel_getSupport: 'Get Support',
btnLabel_updateCheck: 'Check for Updates',
btnLabel_update: 'Update',
btnLabel_dismiss: 'Dismiss',
link_viewChanges: 'View changes',
link_shareFeedback: 'Share Feedback',
prefix_exit: 'Exit',
state_on: 'On',
state_off: 'Off'
}
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 = ( 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) {
try { // to return localized messages.json
const msgs = JSON.parse(resp.responseText), flatMsgs = {}
for (const key in msgs) // remove need to ref nested keys
if (typeof msgs[key] == 'object' && 'message' in msgs[key])
flatMsgs[key] = msgs[key].message
resolve(flatMsgs)
} catch (err) { // if bad response
msgXHRtries++ ; if (msgXHRtries == 3) return resolve({}) // try up to 3X (original/region-stripped/EN) only
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()
}
}
fetchMsgs()
})
Object.assign(app.msgs, localizedMsgs)
}
log.debug(`Success! app.msgs = ${log.prettifyObj(app.msgs)}`)

// Init SETTINGS controls
log.debug('Initializing settings properties...')
Object.assign(settings, { controls: {
Expand Down

0 comments on commit 5748575

Please sign in to comment.