Skip to content

Commit af6130a

Browse files
committed
Moved chatbar from content.js to lib/chatbar.js ↞ [auto-sync from https://github.com/adamlui/ai-web-extensions/tree/main/chatgpt-widescreen]
1 parent aa0dbdc commit af6130a

File tree

6 files changed

+124
-100
lines changed

6 files changed

+124
-100
lines changed

chromium/extension/content.js

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
// Import JS resources
99
for (const resource of [
10-
'lib/chatgpt.js', 'lib/dom.js', 'lib/settings.js', 'components/buttons.js', 'components/modals.js'])
11-
await import(chrome.runtime.getURL(resource))
10+
'lib/chatbar.js', 'lib/chatgpt.js', 'lib/dom.js', 'lib/settings.js',
11+
'components/buttons.js', 'components/modals.js'
12+
]) await import(chrome.runtime.getURL(resource))
1213

1314
// Init ENV context
1415
const env = {
@@ -21,6 +22,7 @@
2122
{ sites } = await chrome.storage.sync.get('sites')
2223

2324
// Export DEPENDENCIES to imported resources
25+
chatbar.imports.import({ env, sites }) // for env.site + sites.selectors
2426
dom.imports.import({ env }) // for env.ui.scheme
2527
modals.imports.import({ app, env }) // for app data + env.ui.scheme
2628
settings.imports.import({ env }) // to load/save active tab's settings using env.site
@@ -77,53 +79,6 @@
7779
}
7880
}
7981

80-
const chatbar = {
81-
82-
get() {
83-
let chatbar = document.querySelector(sites[env.site].selectors.input)
84-
const lvlsToParent = env.site == 'chatgpt' ? 3 : 2
85-
for (let i = 0 ; i < lvlsToParent ; i++) chatbar = chatbar?.parentNode
86-
return chatbar
87-
},
88-
89-
isDark() {
90-
return env.site != 'chatgpt' ? undefined
91-
: getComputedStyle(document.getElementById('composer-background') || document.documentElement)
92-
.backgroundColor == 'rgb(48, 48, 48)'
93-
},
94-
95-
isTall() {
96-
return env.site == 'poe' ? true
97-
: env.site == 'perplexity' ? this.get()?.getBoundingClientRect().height > 60
98-
: /* chatgpt */ !!this.get()?.nextElementSibling
99-
},
100-
101-
tweak() {
102-
if (env.site != 'chatgpt') return
103-
const chatbarDiv = chatbar.get() ; if (!chatbarDiv) return
104-
const inputArea = chatbarDiv.querySelector(sites[env.site].selectors.input) ; if (!inputArea) return
105-
if (chatgpt.canvasIsOpen()) inputArea.parentNode.style.width = '100%'
106-
else if (!env.tallChatbar) { // narrow it to not clash w/ buttons
107-
const widths = { chatbar: chatbarDiv.getBoundingClientRect().width }
108-
const visibleBtnTypes = [...buttons.getTypes.visible(), 'send']
109-
visibleBtnTypes.forEach(type =>
110-
widths[type] = buttons[type]?.getBoundingClientRect().width
111-
|| document.querySelector(`${sites.chatgpt.selectors.btns.send}, ${
112-
sites.chatgpt.selectors.btns.stop}`)?.getBoundingClientRect().width || 0 )
113-
const totalBtnWidths = visibleBtnTypes.reduce((sum, btnType) => sum + widths[btnType], 0)
114-
inputArea.parentNode.style.width = `${ // expand to close gap w/ buttons
115-
widths.chatbar - totalBtnWidths -43 }px`
116-
inputArea.style.width = '100%' // rid h-scrollbar
117-
}
118-
},
119-
120-
reset() { // all tweaks for popup master toggle-off
121-
const chatbarDiv = chatbar.get() ; if (!chatbarDiv) return
122-
const inputArea = chatbarDiv.querySelector(sites.chatgpt.selectors.input)
123-
if (inputArea) inputArea.style.width = inputArea.parentNode.style.width = 'initial'
124-
}
125-
}
126-
12782
const toggle = {
12883

12984
mode(mode, state = '') {

chromium/extension/lib/chatbar.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
window.chatbar = {
2+
3+
imports: {
4+
import(deps) { // { env, sites }
5+
for (const depName in deps) this[depName] = deps[depName] }
6+
},
7+
8+
get() {
9+
const site = this.imports.env.site
10+
let chatbar = document.querySelector(this.imports.sites[site].selectors.input)
11+
const lvlsToParent = site == 'chatgpt' ? 3 : 2
12+
for (let i = 0 ; i < lvlsToParent ; i++) chatbar = chatbar?.parentNode
13+
return chatbar
14+
},
15+
16+
isDark() {
17+
return this.imports.env.site.site != 'chatgpt' ? undefined
18+
: getComputedStyle(document.getElementById('composer-background') || document.documentElement)
19+
.backgroundColor == 'rgb(48, 48, 48)'
20+
},
21+
22+
isTall() {
23+
const site = this.imports.env.site
24+
return site == 'poe' ? true
25+
: site == 'perplexity' ? this.get()?.getBoundingClientRect().height > 60
26+
: /* chatgpt */ !!this.get()?.nextElementSibling
27+
},
28+
29+
tweak() {
30+
const site = this.imports.env.site ; if (site != 'chatgpt') return
31+
const sites = this.imports.sites
32+
const chatbarDiv = this.get() ; if (!chatbarDiv) return
33+
const inputArea = chatbarDiv.querySelector(sites[site].selectors.input) ; if (!inputArea) return
34+
if (chatgpt.canvasIsOpen()) inputArea.parentNode.style.width = '100%'
35+
else if (!this.isTall()) { // narrow it to not clash w/ buttons
36+
const widths = { chatbar: chatbarDiv.getBoundingClientRect().width }
37+
const visibleBtnTypes = [...buttons.getTypes.visible(), 'send']
38+
visibleBtnTypes.forEach(type =>
39+
widths[type] = buttons[type]?.getBoundingClientRect().width
40+
|| document.querySelector(`${sites.chatgpt.selectors.btns.send}, ${
41+
sites.chatgpt.selectors.btns.stop}`)?.getBoundingClientRect().width || 0 )
42+
const totalBtnWidths = visibleBtnTypes.reduce((sum, btnType) => sum + widths[btnType], 0)
43+
inputArea.parentNode.style.width = `${ // expand to close gap w/ buttons
44+
widths.chatbar - totalBtnWidths -43 }px`
45+
inputArea.style.width = '100%' // rid h-scrollbar
46+
}
47+
},
48+
49+
reset() { // all tweaks for popup master toggle-off
50+
const chatbarDiv = this.get() ; if (!chatbarDiv) return
51+
const inputArea = chatbarDiv.querySelector(this.imports.sites.chatgpt.selectors.input)
52+
if (inputArea) inputArea.style.width = inputArea.parentNode.style.width = 'initial'
53+
}
54+
}

chromium/extension/manifest.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"action": { "default_popup": "popup/index.html" },
1919
"web_accessible_resources": [{
2020
"matches": [ "<all_urls>" ],
21-
"resources": [ "components/buttons.js", "components/modals.js", "lib/chatgpt.js", "lib/dom.js", "lib/settings.js" ]
21+
"resources": [
22+
"components/buttons.js", "components/modals.js",
23+
"lib/chatbar.js", "lib/chatgpt.js", "lib/dom.js", "lib/settings.js"
24+
]
2225
}],
2326
"content_scripts": [{
2427
"matches": [ "https://chatgpt.com/*", "https://www.perplexity.ai/*", "https://poe.com/*" ],

firefox/extension/content.js

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
// Import JS resources
99
for (const resource of [
10-
'lib/chatgpt.js', 'lib/dom.js', 'lib/settings.js', 'components/buttons.js', 'components/modals.js'])
11-
await import(chrome.runtime.getURL(resource))
10+
'lib/chatbar.js', 'lib/chatgpt.js', 'lib/dom.js', 'lib/settings.js',
11+
'components/buttons.js', 'components/modals.js'
12+
]) await import(chrome.runtime.getURL(resource))
1213

1314
// Init ENV context
1415
const env = {
@@ -21,6 +22,7 @@
2122
{ sites } = await chrome.storage.sync.get('sites')
2223

2324
// Export DEPENDENCIES to imported resources
25+
chatbar.imports.import({ env, sites }) // for env.site + sites.selectors
2426
dom.imports.import({ env }) // for env.ui.scheme
2527
modals.imports.import({ app, env }) // for app data + env.ui.scheme
2628
settings.imports.import({ env }) // to load/save active tab's settings using env.site
@@ -77,53 +79,6 @@
7779
}
7880
}
7981

80-
const chatbar = {
81-
82-
get() {
83-
let chatbar = document.querySelector(sites[env.site].selectors.input)
84-
const lvlsToParent = env.site == 'chatgpt' ? 3 : 2
85-
for (let i = 0 ; i < lvlsToParent ; i++) chatbar = chatbar?.parentNode
86-
return chatbar
87-
},
88-
89-
isDark() {
90-
return env.site != 'chatgpt' ? undefined
91-
: getComputedStyle(document.getElementById('composer-background') || document.documentElement)
92-
.backgroundColor == 'rgb(48, 48, 48)'
93-
},
94-
95-
isTall() {
96-
return env.site == 'poe' ? true
97-
: env.site == 'perplexity' ? this.get()?.getBoundingClientRect().height > 60
98-
: /* chatgpt */ !!this.get()?.nextElementSibling
99-
},
100-
101-
tweak() {
102-
if (env.site != 'chatgpt') return
103-
const chatbarDiv = chatbar.get() ; if (!chatbarDiv) return
104-
const inputArea = chatbarDiv.querySelector(sites[env.site].selectors.input) ; if (!inputArea) return
105-
if (chatgpt.canvasIsOpen()) inputArea.parentNode.style.width = '100%'
106-
else if (!env.tallChatbar) { // narrow it to not clash w/ buttons
107-
const widths = { chatbar: chatbarDiv.getBoundingClientRect().width }
108-
const visibleBtnTypes = [...buttons.getTypes.visible(), 'send']
109-
visibleBtnTypes.forEach(type =>
110-
widths[type] = buttons[type]?.getBoundingClientRect().width
111-
|| document.querySelector(`${sites.chatgpt.selectors.btns.send}, ${
112-
sites.chatgpt.selectors.btns.stop}`)?.getBoundingClientRect().width || 0 )
113-
const totalBtnWidths = visibleBtnTypes.reduce((sum, btnType) => sum + widths[btnType], 0)
114-
inputArea.parentNode.style.width = `${ // expand to close gap w/ buttons
115-
widths.chatbar - totalBtnWidths -43 }px`
116-
inputArea.style.width = '100%' // rid h-scrollbar
117-
}
118-
},
119-
120-
reset() { // all tweaks for popup master toggle-off
121-
const chatbarDiv = chatbar.get() ; if (!chatbarDiv) return
122-
const inputArea = chatbarDiv.querySelector(sites.chatgpt.selectors.input)
123-
if (inputArea) inputArea.style.width = inputArea.parentNode.style.width = 'initial'
124-
}
125-
}
126-
12782
const toggle = {
12883

12984
mode(mode, state = '') {

firefox/extension/lib/chatbar.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
window.chatbar = {
2+
3+
imports: {
4+
import(deps) { // { env, sites }
5+
for (const depName in deps) this[depName] = deps[depName] }
6+
},
7+
8+
get() {
9+
const site = this.imports.env.site
10+
let chatbar = document.querySelector(this.imports.sites[site].selectors.input)
11+
const lvlsToParent = site == 'chatgpt' ? 3 : 2
12+
for (let i = 0 ; i < lvlsToParent ; i++) chatbar = chatbar?.parentNode
13+
return chatbar
14+
},
15+
16+
isDark() {
17+
return this.imports.env.site.site != 'chatgpt' ? undefined
18+
: getComputedStyle(document.getElementById('composer-background') || document.documentElement)
19+
.backgroundColor == 'rgb(48, 48, 48)'
20+
},
21+
22+
isTall() {
23+
const site = this.imports.env.site
24+
return site == 'poe' ? true
25+
: site == 'perplexity' ? this.get()?.getBoundingClientRect().height > 60
26+
: /* chatgpt */ !!this.get()?.nextElementSibling
27+
},
28+
29+
tweak() {
30+
const site = this.imports.env.site ; if (site != 'chatgpt') return
31+
const sites = this.imports.sites
32+
const chatbarDiv = this.get() ; if (!chatbarDiv) return
33+
const inputArea = chatbarDiv.querySelector(sites[site].selectors.input) ; if (!inputArea) return
34+
if (chatgpt.canvasIsOpen()) inputArea.parentNode.style.width = '100%'
35+
else if (!this.isTall()) { // narrow it to not clash w/ buttons
36+
const widths = { chatbar: chatbarDiv.getBoundingClientRect().width }
37+
const visibleBtnTypes = [...buttons.getTypes.visible(), 'send']
38+
visibleBtnTypes.forEach(type =>
39+
widths[type] = buttons[type]?.getBoundingClientRect().width
40+
|| document.querySelector(`${sites.chatgpt.selectors.btns.send}, ${
41+
sites.chatgpt.selectors.btns.stop}`)?.getBoundingClientRect().width || 0 )
42+
const totalBtnWidths = visibleBtnTypes.reduce((sum, btnType) => sum + widths[btnType], 0)
43+
inputArea.parentNode.style.width = `${ // expand to close gap w/ buttons
44+
widths.chatbar - totalBtnWidths -43 }px`
45+
inputArea.style.width = '100%' // rid h-scrollbar
46+
}
47+
},
48+
49+
reset() { // all tweaks for popup master toggle-off
50+
const chatbarDiv = this.get() ; if (!chatbarDiv) return
51+
const inputArea = chatbarDiv.querySelector(this.imports.sites.chatgpt.selectors.input)
52+
if (inputArea) inputArea.style.width = inputArea.parentNode.style.width = 'initial'
53+
}
54+
}

firefox/extension/manifest.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"action": { "default_popup": "popup/index.html" },
1919
"web_accessible_resources": [{
2020
"matches": [ "<all_urls>" ],
21-
"resources": [ "components/buttons.js", "components/modals.js", "lib/chatgpt.js", "lib/dom.js", "lib/settings.js" ]
21+
"resources": [
22+
"components/buttons.js", "components/modals.js",
23+
"lib/chatbar.js", "lib/chatgpt.js", "lib/dom.js", "lib/settings.js"
24+
]
2225
}],
2326
"content_scripts": [{
2427
"matches": [ "https://chatgpt.com/*", "https://www.perplexity.ai/*", "https://poe.com/*" ],

0 commit comments

Comments
 (0)