Skip to content

Commit c7b1317

Browse files
committed
fix: bring the pin back on chatgpt
1 parent e637e90 commit c7b1317

File tree

1 file changed

+58
-50
lines changed

1 file changed

+58
-50
lines changed

src/contents/pin.tsx

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import PinIcon from "~common/pin"
1414

1515
import "~styles.css"
1616

17-
import { cp } from "fs"
1817
import { useCallback, useEffect, useState } from "react"
1918

2019
import LogoIcon from "~common/logo"
@@ -26,38 +25,40 @@ export const config: PlasmoCSConfig = {
2625
matches: ["https://chat.openai.com/*", "https://chatgpt.com/*"]
2726
}
2827

28+
/**
29+
* Previously this was querying for the <markdown> tag.
30+
* The ChatGPT DOM uses a div with a "markdown" class.
31+
*/
2932
export const getInlineAnchorList: PlasmoGetInlineAnchorList = async () =>
30-
document.querySelectorAll("div > .pt-0")
33+
document.querySelectorAll(".markdown")
3134

3235
export const render: PlasmoRender<Element> = async ({
3336
anchor, // the observed anchor, OR document.body.
34-
createRootContainer // This creates the default root container
37+
createRootContainer // Creates the default root container
3538
}) => {
3639
if (!anchor || !createRootContainer) return
37-
// const parentElement = anchor?.element.parentElement?.parentElement
38-
// if (!parentElement) return
39-
// const parentAnchor = {
40-
// element: parentElement,
41-
// type: anchor.type
42-
// }
43-
44-
// console.log({ anchor, parentAnchor })
45-
const rootContainer = await createRootContainer(anchor)
4640

47-
const root = createRoot(rootContainer) // Any root
48-
const parent =
49-
anchor?.element?.parentElement?.parentElement?.parentElement?.parentElement
41+
const rootContainer = await createRootContainer(anchor)
42+
// Instead of multiple parentElement calls, use closest() to grab the container
43+
// that represents the whole assistant turn.
44+
const parent = anchor.element.closest(".agent-turn")
5045
parent?.classList.add("pin")
46+
parent?.insertBefore(rootContainer, parent.firstChild)
47+
48+
const root = createRoot(rootContainer)
49+
rootContainer.classList.add("relative")
50+
5151
root.render(
5252
<>
53-
<Content
54-
// @ts-ignore
55-
parent={parent}
56-
/>
53+
<Content parent={parent} />
5754
</>
5855
)
5956
}
6057

58+
type Props = {
59+
parent: Element | null
60+
}
61+
6162
const Content = ({ parent }: Props) => {
6263
const [toBeSaved, setToBeSaved] = useStorage<ToBeSaved>(
6364
STORAGE_KEYS.toBeSaved
@@ -81,11 +82,19 @@ const Content = ({ parent }: Props) => {
8182
const [pinIndex, setPinIndex] = useState(-1)
8283

8384
useEffect(() => {
84-
if (parent.parentElement?.querySelector(".agent-turn")) setShowPin(true)
85-
else parent.classList.remove("pin")
85+
console.log(parent)
86+
}, [parent])
87+
88+
useEffect(() => {
89+
// Only show the pin if a sibling agent-turn exists
90+
if (parent?.parentElement?.querySelector(".agent-turn")) {
91+
setShowPin(true)
92+
} else {
93+
parent?.classList.remove("pin")
94+
}
8695
const index = getPinIndex(parent)
8796
setPinIndex(index)
88-
}, [])
97+
}, [parent])
8998

9099
useEffect(() => {
91100
if (!(isPremium || activeTrial) || !chatID) return
@@ -101,17 +110,13 @@ const Content = ({ parent }: Props) => {
101110
const pins = document.querySelectorAll(".pin")
102111
const lastPin = pins[pins.length - 1]
103112
if (!lastPin) return false
104-
// Unelegant way to get the node of an element
105-
const parentNode = parent.firstChild?.parentNode
113+
// A somewhat inelegant way to compare nodes:
114+
const parentNode = parent?.firstChild?.parentNode
106115
if (!parentNode) return false
107-
return (
108-
lastPin.firstChild?.parentNode?.isSameNode(
109-
parent.firstChild?.parentNode
110-
) ?? false
111-
)
116+
return lastPin.firstChild?.parentNode?.isSameNode(parentNode) ?? false
112117
})()
113118
)
114-
}, [chatID, status])
119+
}, [chatID, status, isPremium, activeTrial, parent])
115120

116121
const LastMessageIcon = useCallback(() => {
117122
switch (status) {
@@ -125,28 +130,31 @@ const Content = ({ parent }: Props) => {
125130
return <LogoIcon error />
126131
case "saved":
127132
return <LogoIcon />
133+
default:
134+
return <LogoIcon />
128135
}
129-
}, [chatID, status])
136+
}, [status])
130137

131138
const handleClick = async () => {
132139
if (!authenticated) {
133140
alert(i18n("errConnect"))
134141
return
135142
}
136143

137-
const images = Array.from(parent.querySelectorAll("img") || [])
138-
const text = Array.from(parent.querySelectorAll(".markdown")).map((el) =>
139-
el.parentElement?.classList.contains("mt-3")
140-
? "%%CHATGPT_TO_NOTION_WORK1%%" + el.innerHTML
141-
: el.innerHTML
144+
const images = Array.from(parent?.querySelectorAll("img") || [])
145+
const text = Array.from(parent?.querySelectorAll(".markdown") || []).map(
146+
(el) =>
147+
el.parentElement?.classList.contains("mt-3")
148+
? "%%CHATGPT_TO_NOTION_WORK1%%" + el.innerHTML
149+
: el.innerHTML
142150
)
143151

144152
console.log(text)
145153

146154
let uncompressedAnswer = text[0]
147155
if (text.length > 1) {
148156
uncompressedAnswer = text.reduce((acc, curr, i, arr) => {
149-
if (i == 0) return curr
157+
if (i === 0) return curr
150158

151159
const prev = arr[i - 1]
152160
const joint = (curr + prev).includes("%%CHATGPT_TO_NOTION_WORK2%%")
@@ -162,12 +170,13 @@ const Content = ({ parent }: Props) => {
162170
: uncompressedAnswer
163171
const answer = await compress(preCompressionAnswer)
164172

165-
const prompt = await compress(
166-
// @ts-ignore
167-
parent.parentElement.previousElementSibling.querySelector(
173+
// Find the prompt in the previous sibling element:
174+
const promptElement =
175+
parent?.parentElement?.previousElementSibling?.querySelector(
168176
".whitespace-pre-wrap"
169-
).textContent
170-
)
177+
)
178+
const promptText = promptElement ? promptElement.textContent : ""
179+
const prompt = await compress(promptText || "")
171180
const title = document.title
172181
const url = window.location.href
173182

@@ -201,7 +210,6 @@ const Content = ({ parent }: Props) => {
201210
background: "transparent",
202211
border: "none",
203212
marginTop: 10,
204-
width: "100%",
205213
cursor: status !== "generating" ? "pointer" : "default"
206214
}}>
207215
{isLastMessage ? <LastMessageIcon /> : <LogoIcon />}
@@ -218,7 +226,9 @@ const Content = ({ parent }: Props) => {
218226
marginTop: 10,
219227
padding: 4,
220228
borderRadius: 4,
221-
width: "100%",
229+
position: "absolute",
230+
top: 32,
231+
left: -42,
222232
cursor: "pointer"
223233
}}>
224234
<PinIcon />
@@ -228,11 +238,9 @@ const Content = ({ parent }: Props) => {
228238

229239
export default Content
230240

231-
const getPinIndex = (parent: Element) => {
241+
const getPinIndex = (parent: Element | null) => {
232242
const pins = document.querySelectorAll(".pin")
233-
return Array.from(pins).findIndex((pin) => pin.isSameNode(parent))
234-
}
235-
236-
type Props = {
237-
parent: Element
243+
return Array.from(pins).findIndex((pin) =>
244+
parent ? pin.isSameNode(parent) : false
245+
)
238246
}

0 commit comments

Comments
 (0)