Skip to content

Commit 84ac066

Browse files
committed
Added app.cssPrefix to avoid potential selector pollution + modal styles ↞ [auto-sync from https://github.com/KudoAI/chatgpt.js-chrome-starter]
1 parent 3a4df0e commit 84ac066

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

starters/chrome/extension/components/modals.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
window.modals = {
44
stack: [], // of types of undismissed modals
5+
get class() { return `${this.dependencies.app.cssPrefix}-modal` },
56

67
dependencies: {
7-
import(dependencies) { // { app }
8+
import(dependencies) { // { app, env }
89
for (const name in dependencies) this[name] = dependencies[name] }
910
},
1011

1112
alert(title = '', msg = '', btns = '', checkbox = '', width = '') {
1213
const alertID = chatgpt.alert(title, msg, btns, checkbox, width),
1314
alert = document.getElementById(alertID).firstChild
15+
this.init(alert) // add class
1416
return alert
1517
},
1618

@@ -23,6 +25,54 @@ window.modals = {
2325
this.observeRemoval(modal, modalType) // to maintain stack for proper nav
2426
},
2527

28+
init(modal) {
29+
if (!this.styles) this.stylize() // to init/append stylesheet
30+
modal.classList.add(this.class) ; modal.parentNode.classList.add(`${this.class}-bg`) // add classes
31+
},
32+
33+
stylize() {
34+
if (!this.styles) {
35+
this.styles = document.createElement('style') ; this.styles.id = `${this.class}-styles`
36+
document.head.append(this.styles)
37+
}
38+
this.styles.innerText = (
39+
`.${this.class} {` // modals
40+
+ 'font-family: -apple-system, system-ui, BlinkMacSystemFont, Segoe UI, Roboto,'
41+
+ 'Oxygen-Sans, Ubuntu, Cantarell, Helvetica Neue, sans-serif ;'
42+
+ 'padding: 20px 25px 24px 25px !important ; font-size: 20px ;'
43+
+ `color: ${ this.dependencies.env.scheme == 'dark' ? 'white' : 'black' } !important ;`
44+
+ `background-image: linear-gradient(180deg, ${
45+
this.dependencies.env.scheme == 'dark' ? '#99a8a6 -200px, black 200px'
46+
: '#b6ebff -296px, white 171px' }) }`
47+
+ `.${this.class} [class*=modal-close-btn] {`
48+
+ 'position: absolute !important ; float: right ; top: 14px !important ; right: 16px !important ;'
49+
+ 'cursor: pointer ; width: 33px ; height: 33px ; border-radius: 20px }'
50+
+ `.${this.class} [class*=modal-close-btn] svg { height: 10px }`
51+
+ `.${this.class} [class*=modal-close-btn] path {`
52+
+ `${ this.dependencies.env.scheme == 'dark' ? 'stroke: white ; fill: white'
53+
: 'stroke: #9f9f9f ; fill: #9f9f9f' }}`
54+
+ ( this.dependencies.env.scheme == 'dark' ? // invert dark mode hover paths
55+
`.${this.class} [class*=modal-close-btn]:hover path { stroke: black ; fill: black }` : '' )
56+
+ `.${this.class} [class*=modal-close-btn]:hover { background-color: #f2f2f2 }` // hover underlay
57+
+ `.${this.class} [class*=modal-close-btn] svg { margin: 11.5px }` // center SVG for hover underlay
58+
+ `.${this.class} a { color: #${ this.dependencies.env.scheme == 'dark' ? '00cfff' : '1e9ebb' } !important }`
59+
+ `.${this.class} h2 { font-weight: bold }`
60+
+ `.${this.class} button {`
61+
+ 'font-size: 14px ; text-transform: uppercase ;' // shrink/uppercase labels
62+
+ 'border-radius: 0 !important ;' // square borders
63+
+ 'transition: transform 0.1s ease-in-out, box-shadow 0.1s ease-in-out ;' // smoothen hover fx
64+
+ 'cursor: pointer !important ;' // add finger cursor
65+
+ `border: 1px solid ${ this.dependencies.env.scheme == 'dark' ? 'white' : 'black' } !important ;`
66+
+ 'padding: 8px !important ; min-width: 102px }' // resize
67+
+ `.${this.class} button:hover {` // add zoom, re-scheme
68+
+ 'transform: scale(1.055) ; color: black !important ;'
69+
+ `background-color: #${ this.dependencies.env.scheme == 'dark' ? '00cfff' : '9cdaff' } !important }`
70+
+ ( !this.dependencies.env.browser.isMobile ?
71+
`.${this.class} .modal-buttons { margin-left: -13px !important }` : '' )
72+
+ `.about-em { color: ${ this.dependencies.env.scheme == 'dark' ? 'white' : 'green' } !important }`
73+
)
74+
},
75+
2676
observeRemoval(modal, modalType) { // to maintain stack for proper nav
2777
const modalBG = modal.parentNode
2878
new MutationObserver(([mutation], obs) => {

starters/chrome/extension/content.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
for (const resource of ['components/modals.js', 'lib/chatgpt.js', 'lib/dom.js', 'lib/settings.js'])
88
await import(chrome.runtime.getURL(resource))
99

10+
// Init ENV context
11+
const env = { scheme: getScheme() }}
12+
1013
// Import APP data
1114
const { app } = await chrome.storage.sync.get('app')
12-
modals.dependencies.import({ app })
15+
modals.dependencies.import({ app, env }) // for app data + env.scheme
1316

1417
// Add CHROME MSG listener
1518
chrome.runtime.onMessage.addListener(req => { // from service-worker.js + popup/index.html
@@ -24,7 +27,7 @@
2427
// Init SETTINGS
2528
await settings.load(Object.keys(settings.controls), 'skipAlert')
2629

27-
// Define FEEDBACK functions
30+
// Define FUNCTIONS
2831

2932
function notify(msg, pos = '', notifDuration = '', shadow = '') {
3033

@@ -47,8 +50,6 @@
4750
}
4851
}
4952

50-
// Define SYNC function
51-
5253
async function syncConfigToUI(options) { // eslint-disable-line
5354
await settings.load('extensionDisabled', Object.keys(settings.controls)) // load from Chrome storage to content.js config
5455
if (config.extensionDisabled) {
@@ -62,6 +63,11 @@
6263
}
6364
}
6465

66+
function getScheme() {
67+
return document.documentElement.className
68+
|| (window.matchMedia?.('(prefers-color-scheme: dark)')?.matches ? 'dark' : 'light')
69+
}
70+
6571
// Run MAIN routine
6672

6773
chatgpt.printAllFunctions() // to console

starters/chrome/extension/service-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Init APP data
22
const app = {
3-
symbol: '🤖', version: chrome.runtime.getManifest().version,
3+
version: chrome.runtime.getManifest().version, symbol: '🤖', cssPrefix: 'chatgpt-extension',
44
urls: {
55
assetHost: 'https://cdn.jsdelivr.net/gh/KudoAI/chatgpt.js-chrome-starter',
66
cjsMediaHost: 'https://media.chatgptjs.org',

0 commit comments

Comments
 (0)