-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (50 loc) · 1.42 KB
/
Copy pathscript.js
File metadata and controls
56 lines (50 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const turndownService = new TurndownService({
codeBlockStyle: "fenced"
})
turndownService.remove("button")
async function getUrl() {
const storage = await browser.storage.local.get()
return storage.webhookUrl
}
async function forward(div) {
const markdown = turndownService.turndown(div)
const sections = markdown.split("```")
const msg = {
lang: sections[0],
code: sections[1]
}
browser.runtime.sendMessage({
action: 'forward',
msg,
url: await getUrl()
})
}
function createObserver() {
const targetNode = document.querySelector("main")
const config = { attributes: false, childList: true, subtree: true }
const callback = (mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === "childList") {
if (mutation.target.id === "last-reply-container") {
if (mutation.target.querySelector(".action-buttons") != null) {
if (mutation.addedNodes.length === 1) {
console.log("grok-webhook:observed")
setTimeout(() => mutation.target.querySelectorAll("[data-testid]").forEach(div => forward(div)), 1000)
}
}
}
}
}
}
const observer = new MutationObserver(callback)
observer.observe(targetNode, config)
return observer
}
async function printWebhookUrl() {
const storage = await browser.storage.local.get()
console.log(storage.webhookUrl)
}
setTimeout(() => {
let observer = createObserver()
printWebhookUrl()
}, 1000)