Skip to content

Commit

Permalink
Separated static app data from config for readability ↞ [auto-sync …
Browse files Browse the repository at this point in the history
…from `adamlui/chatgpt-apps`]
  • Loading branch information
adamlui authored and kudo-sync-bot committed Sep 7, 2024
1 parent 62f64c0 commit 01d037c
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions greasemonkey/googlegpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
// @description:zu Yengeza izimpendulo ze-AI ku-Google Search (inikwa amandla yi-Google Gemma + GPT-4o!)
// @author KudoAI
// @namespace https://kudoai.com
// @version 2024.9.6.1
// @version 2024.9.7
// @license MIT
// @icon https://media.googlegpt.io/images/icons/googlegpt/black/icon48.png?8652a6e
// @icon64 https://media.googlegpt.io/images/icons/googlegpt/black/icon64.png?8652a6e
Expand Down Expand Up @@ -412,6 +412,19 @@

(async () => {

// Init APP INFO
const app = {
name: 'GoogleGPT', symbol: '🤖', configKeyPrefix: 'googleGPT',
urls: {
app: 'https://www.googlegpt.io', support: 'https://support.googlegpt.io',
gitHub: 'https://github.com/KudoAI/googlegpt',
greasyFork: 'https://greasyfork.org/scripts/478597-googlegpt' },
latestAssetCommitHash: '4ef557c' // for cached messages.json
}
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-zA-Z-]*)$/, (_, id, name) => `${id}/${ !name ? 'script' : name }.meta.js`)

// Init browser/compatibility FLAGS
const isChrome = chatgpt.browser.isChrome(),
isFirefox = chatgpt.browser.isFirefox(),
Expand All @@ -423,16 +436,7 @@
userscriptManager: /Tampermonkey|ScriptCat/.test(getUserscriptManager()) }

// Init CONFIG
const config = {
appName: 'GoogleGPT', appSymbol: '🤖', keyPrefix: 'googleGPT',
appURL: 'https://www.googlegpt.io', gitHubURL: 'https://github.com/KudoAI/googlegpt',
greasyForkURL: 'https://greasyfork.org/scripts/478597-googlegpt',
minFontSize: 11, maxFontSize: 24, lineHeightRatio: isMobile ? 1.357 : 1.375,
latestAssetCommitHash: '4ef557c' } // for cached messages.json
config.updateURL = config.greasyForkURL.replace('https://', 'https://update.')
.replace(/(\d+)-?([a-zA-Z-]*)$/, (_, id, name) => `${ id }/${ !name ? 'script' : name }.meta.js`)
config.supportURL = config.gitHubURL + '/issues/new'
config.assetHostURL = config.gitHubURL.replace('github.com', 'cdn.jsdelivr.net/gh') + `@${config.latestAssetCommitHash}/`
const config = { minFontSize: 11, maxFontSize: 24, lineHeightRatio: isMobile ? 1.357 : 1.375 }
config.userLanguage = chatgpt.getUserLanguage()
config.userLocale = window.location.hostname.endsWith('.com') ? 'us'
: window.location.hostname.split('.').pop()
Expand Down Expand Up @@ -500,7 +504,7 @@
// Init MESSAGES
let msgs = {}
if (!config.userLanguage.startsWith('en')) msgs = await new Promise(resolve => {
const msgHostDir = config.assetHostURL + 'greasemonkey/_locales/',
const msgHostDir = app.urls.assetHost + 'greasemonkey/_locales/',
msgLocaleDir = ( config.userLanguage ? config.userLanguage.replace('-', '_') : 'en' ) + '/'
let msgHref = msgHostDir + msgLocaleDir + 'messages.json', msgXHRtries = 0
xhr({ method: 'GET', url: msgHref, onload: onLoad })
Expand Down Expand Up @@ -569,7 +573,7 @@
label: msgs.menuLabel_colorScheme || 'Color Scheme',
helptip: msgs.helptip_colorScheme || 'Scheme to display GoogleGPT UI components in' },
about: { type: 'modal', icon: 'questionMarkCircle',
label: `${ msgs.menuLabel_about || 'About' } ${config.appName}...` }
label: `${ msgs.menuLabel_about || 'About' } ${app.name}...` }
}

// Init MENU objs
Expand All @@ -581,8 +585,8 @@

// Define SCRIPT functions

function loadSetting(...keys) { keys.forEach(key => config[key] = GM_getValue(config.keyPrefix + '_' + key, false)) }
function saveSetting(key, value) { GM_setValue(config.keyPrefix + '_' + key, value) ; config[key] = value }
function loadSetting(...keys) { keys.forEach(key => config[key] = GM_getValue(app.configKeyPrefix + '_' + key, false)) }
function saveSetting(key, value) { GM_setValue(app.configKeyPrefix + '_' + key, value) ; config[key] = value }
function safeWindowOpen(url) { window.open(url, '_blank', 'noopener') } // to prevent backdoor vulnerabilities
function getUserscriptManager() { try { return GM_info.scriptHandler } catch (err) { return 'other' }}

Expand Down Expand Up @@ -616,7 +620,7 @@
: replyLanguage.charAt(0).toUpperCase() + replyLanguage.slice(1).toLowerCase() )
saveSetting('replyLanguage', replyLanguage || config.userLanguage)
const langUpdatedAlertID = siteAlert(( msgs.alert_langUpdated || 'Language updated' ) + '!', // title
`${ config.appName } ${ msgs.alert_willReplyIn || 'will reply in' } `
`${ app.name } ${ msgs.alert_willReplyIn || 'will reply in' } `
+ ( replyLanguage || msgs.alert_yourSysLang || 'your system language' ) + '.',
'', '', 330) // confirmation width
const langUpdatedAlert = document.getElementById(langUpdatedAlertID).firstChild
Expand All @@ -636,7 +640,7 @@
// Fetch latest meta
const currentVer = GM_info.script.version
xhr({
method: 'GET', url: config.updateURL + '?t=' + Date.now(),
method: 'GET', url: app.urls.update + '?t=' + Date.now(),
headers: { 'Cache-Control': 'no-cache' },
onload: resp => { const updateAlertWidth = 377

Expand All @@ -650,14 +654,14 @@

// Alert to update
const updateModalID = siteAlert(`🚀 ${ msgs.alert_updateAvail || 'Update available' }!`, // title
`${ msgs.alert_newerVer || 'An update to' } ${ config.appName } `
`${ msgs.alert_newerVer || 'An update to' } ${ app.name } `
+ `(v${ latestVer }) ${ msgs.alert_isAvail || 'is available' }! `
+ '<a target="_blank" rel="noopener" style="font-size: 0.97rem" '
+ 'href="' + config.gitHubURL + '/commits/main/greasemonkey/'
+ config.updateURL.replace(/.*\/(.*)meta\.js/, '$1user.js') + '"'
+ 'href="' + app.urls.gitHub + '/commits/main/greasemonkey/'
+ app.urls.update.replace(/.*\/(.*)meta\.js/, '$1user.js') + '"'
+ `>${ msgs.link_viewChanges || 'View changes' }</a>`,
function update() { // button
safeWindowOpen(config.updateURL.replace('meta.js', 'user.js') + '?t=' + Date.now())
safeWindowOpen(app.urls.update.replace('meta.js', 'user.js') + '?t=' + Date.now())
}, '', updateAlertWidth
)
const updateModal = document.getElementById(updateModalID).firstChild
Expand All @@ -677,7 +681,7 @@

// Alert to no update found, nav back
const noUpdateModalID = siteAlert(( msgs.alert_upToDate || 'Up-to-date' ) + '!', // title
`${ config.appName } (v${ currentVer }) ${ msgs.alert_isUpToDate || 'is up-to-date' }!`, // msg
`${ app.name } (v${ currentVer }) ${ msgs.alert_isUpToDate || 'is up-to-date' }!`, // msg
'', '', updateAlertWidth)
const noUpdateModal = document.getElementById(noUpdateModalID).firstChild
modals.init(noUpdateModal) // add classes/stars, disable wheel-scrolling, dim bg, glowup btns
Expand Down Expand Up @@ -768,13 +772,13 @@

info(label, msg) { // eslint-disable-line
const args = Array.from(arguments).map(arg => typeof arg == 'object' ? JSON.stringify(arg) : arg)
console.info(`${config.appSymbol} ${config.appName} » ${ log.prefix || '' }${
console.info(`${app.symbol} ${app.name} » ${ log.prefix || '' }${
args[0]}${ args[1] ? `: ${args[1]}` : ''}`)
},

err(label, msg) { // eslint-disable-line
const args = Array.from(arguments).map(arg => typeof arg == 'object' ? JSON.stringify(arg) : arg)
console.error(`${config.appSymbol} ${config.appName} » ${ log.prefix || '' }${
console.error(`${app.symbol} ${app.name} » ${ log.prefix || '' }${
args[0]}${ args[1] ? `: ${args[1]}` : ''}`)
}
}
Expand Down Expand Up @@ -861,11 +865,11 @@
+ '<a href="https://chatgpt.js.org" target="_blank" rel="noopener">chatgpt.js</a>'
+ ( chatgptJSver ? ( ' v' + chatgptJSver ) : '' ) + '\n'
+ '📜 ' + ( msgs.about_sourceCode || 'Source code' )
+ `: <a href="${ config.gitHubURL }" target="_blank" rel="nopener">`
+ config.gitHubURL + '</a>',
+ `: <a href="${ app.urls.gitHub }" target="_blank" rel="nopener">`
+ app.urls.gitHub + '</a>',
[ // buttons
function checkForUpdates() { updateCheck() },
function getSupport() { safeWindowOpen(config.supportURL) },
function getSupport() { safeWindowOpen(app.urls.support) },
function leaveAReview() { modals.feedback.show({ sites: 'review' }) },
function moreChatGPTapps() { safeWindowOpen('https://github.com/adamlui/chatgpt-apps') }
], '', 585) // modal width
Expand Down Expand Up @@ -907,7 +911,7 @@
// Init buttons
let btns = [
function greasyFork() { safeWindowOpen(
config.greasyForkURL + '/feedback#post-discussion') },
app.urls.greasyFork + '/feedback#post-discussion') },
function productHunt() { safeWindowOpen(
'https://www.producthunt.com/products/duckduckgpt/reviews/new') },
function futurepedia() { safeWindowOpen(
Expand All @@ -917,7 +921,7 @@
]
if (options.sites == 'feedback') btns.splice(1, 0,
function github() { safeWindowOpen(
config.gitHubURL + '/discussions/new/choose') })
app.urls.gitHub + '/discussions/new/choose') })

// Create/show modal
const feedbackModalID = siteAlert(`${
Expand Down Expand Up @@ -950,7 +954,7 @@

// Create/init modal
const schemeModalID = siteAlert(`${
config.appName } ${( msgs.menuLabel_colorScheme || 'Color Scheme' ).toLowerCase() }:`, '',
app.name } ${( msgs.menuLabel_colorScheme || 'Color Scheme' ).toLowerCase() }:`, '',
[ function auto() {}, function light() {}, function dark() {} ]) // buttons
const schemeModal = document.getElementById(schemeModalID).firstChild

Expand Down Expand Up @@ -2068,7 +2072,7 @@

// Init vars
let chosenAdvertiser, adSelected
const re_appName = new RegExp(config.appName.toLowerCase(), 'i')
const re_appName = new RegExp(app.name.toLowerCase(), 'i')
const currentDate = (() => { // in YYYYMMDD format
const today = new Date(), year = today.getFullYear(),
month = String(today.getMonth() + 1).padStart(2, '0'),
Expand Down Expand Up @@ -2117,7 +2121,7 @@
if (destinationURL.includes('http')) { // insert UTM tags
const [baseURL, queryString] = destinationURL.split('?'),
queryParams = new URLSearchParams(queryString || '')
queryParams.set('utm_source', config.appName.toLowerCase())
queryParams.set('utm_source', app.name.toLowerCase())
queryParams.set('utm_content', 'app_footer_link')
destinationURL = baseURL + '?' + queryParams.toString()
}
Expand Down Expand Up @@ -2763,7 +2767,7 @@
}

function deleteOpenAIcookies() {
GM_deleteValue(config.keyPrefix + '_openAItoken')
GM_deleteValue(app.configKeyPrefix + '_openAItoken')
if (getUserscriptManager() != 'Tampermonkey') return
GM_cookie.list({ url: apis.OpenAI.endpoints.auth }, (cookies, error) => {
if (!error) { for (const cookie of cookies) {
Expand All @@ -2772,15 +2776,15 @@

function getOpenAItoken() {
return new Promise(resolve => {
const accessToken = GM_getValue(config.keyPrefix + '_openAItoken')
const accessToken = GM_getValue(app.configKeyPrefix + '_openAItoken')
log.info('OpenAI access token', accessToken)
if (!accessToken) {
xhr({ url: apis.OpenAI.endpoints.session, onload: resp => {
if (isBlockedbyCloudflare(resp.responseText)) {
appAlert('checkCloudflare') ; return }
try {
const newAccessToken = JSON.parse(resp.responseText).accessToken
GM_setValue(config.keyPrefix + '_openAItoken', newAccessToken)
GM_setValue(app.configKeyPrefix + '_openAItoken', newAccessToken)
resolve(newAccessToken)
} catch { if (get.reply.api == 'OpenAI') appAlert('login') ; return }
}})
Expand Down Expand Up @@ -3215,7 +3219,7 @@
appHeaderLogo.width = isMobile ? 177 : isFirefox ? 124 : 122
appHeaderLogo.style.cssText = `position: relative ; top: ${ isMobile ? 4 : isFirefox ? 3 : 2 }px`
+ ( isMobile ? '; margin-left: 1px' : '' )
const appTitleAnchor = createAnchor(config.appURL, appHeaderLogo)
const appTitleAnchor = createAnchor(app.urls.app, appHeaderLogo)
appTitleAnchor.classList.add('app-name', 'no-user-select')
appDiv.append(appTitleAnchor)

Expand Down Expand Up @@ -3309,7 +3313,7 @@
if (answer == 'standby') {
const standbyBtn = document.createElement('button')
standbyBtn.classList.add('standby-btn', 'no-mobile-tap-outline')
standbyBtn.textContent = msgs.btnLabel_sendQueryToApp || `Send search query to ${config.appName}`
standbyBtn.textContent = msgs.btnLabel_sendQueryToApp || `Send search query to ${app.name}`
appDiv.append(standbyBtn)
show.reply.standbyBtnClickHandler = function() {
appAlert('waitingResponse')
Expand Down Expand Up @@ -3495,7 +3499,7 @@

// Init ALERTS
const appAlerts = {
waitingResponse: `${ msgs.alert_waitingFor || 'Waiting for' } ${config.appName} ${ msgs.alert_response || 'response' }...`,
waitingResponse: `${ msgs.alert_waitingFor || 'Waiting for' } ${app.name} ${ msgs.alert_response || 'response' }...`,
login: `${ msgs.alert_login || 'Please login' } @ `,
checkCloudflare: `${ msgs.alert_checkCloudflare || 'Please pass Cloudflare security check' } @ `,
tooManyRequests: `${ msgs.alert_tooManyRequests || 'API is flooded with too many requests' }.`,
Expand Down

0 comments on commit 01d037c

Please sign in to comment.