forked from kalviumcommunity/ekalvia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
172 lines (161 loc) · 5.51 KB
/
content.js
File metadata and controls
172 lines (161 loc) · 5.51 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
if (typeof browser === 'undefined') {
var browser = chrome
}
browser.runtime.onMessage.addListener(loadPrompt)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
window.postMessage(message, '*')
})
let handler = document.addEventListener('click', e => {
const origin = e.target.closest(`a`)
if (origin && origin.href && origin.href.startsWith('prompt://')) {
e.preventDefault()
browser.runtime.sendMessage(
{ prompt: decodeURIComponent(origin.href.slice(9)), type: 'open' },
function (e) {
console.log('Received ', e)
}
)
}
})
function makeToast(text, toastcolor = '#f0f4f9') {
const smileyDiv = document.createElement('div')
smileyDiv.style.position = 'absolute'
smileyDiv.style.top = '10px' // Set top to 0 for top of the screen
smileyDiv.style.left = '50%'
smileyDiv.style.transform = 'translate(-50%, 0)' // Adjust for horizontal centering
smileyDiv.style.backgroundColor = toastcolor
smileyDiv.style.borderRadius = '15px'
smileyDiv.style.padding = '10px'
smileyDiv.style.zIndex = 9999
smileyDiv.style['font-family'] = 'sans-serif'
// Add smiley text
smileyDiv.textContent = text
document.body.appendChild(smileyDiv)
return smileyDiv
}
window.makeToast = makeToast
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function setLogoText(text = 'Learn ++') {
//gemini
if (window.location.hostname == 'gemini.google.com') {
let logoTextEl = document.querySelector('.logoText')
if (!logoTextEl) {
document.querySelector('.logo').innerHTML +=
'<span class="logoText"></span>'
logoTextEl = document.querySelector('.logoText')
}
logoTextEl.innerHTML = text
return
} else if (window.location.hostname == 'chatgpt.com') {
//chatgpt
await timeout(5000)
let logoTextEl = document.querySelector('.logoText')
if (!logoTextEl) {
document.querySelector(
'span.text-token-text-secondary'
).parentElement.innerHTML += '<span class="logoText"></span>'
logoTextEl = document.querySelector('.logoText')
}
logoTextEl.innerHTML = text
}
}
async function loadPrompt(message) {
await timeout(1000)
if (message.type == 'injectprompt') {
let icon = makeToast('Injecting..')
let inject = injectTextToEditor(message.prompt)
if (!inject) {
console.log(
'unsupported site, or injecting prompt is broken. report an issue!'
)
return false
}
await timeout(500)
setLogoText(' 🏹 Learn with Ekalviya')
icon.remove()
return true
}
}
async function injectTextToEditor(text) {
if (window.location.hostname == 'gemini.google.com') {
const editorElement = document.getElementsByClassName('ql-editor')[0]
let htmltext = text
.split('\n')
.map(t => `<p>${t}</p>`)
.join('')
// Check if element exists before proceeding
if (!editorElement) {
let t = makeToast(
'! Error in prompting automation. Try it again, or report an issue !',
'#ffcccc'
)
await timeout(3000)
t.remove()
return false
}
// Inject the text into the editor
let fill = text
editorElement.innerHTML = fill
await timeout(100)
document.getElementsByClassName('send-button')[0].click()
await timeout(1000)
return true
} else if (window.location.hostname == 'chatgpt.com') {
await 1000
const textElement = document.getElementById('prompt-textarea')
await timeout(1000)
if (textElement) {
try {
textElement.focus()
document.execCommand('insertText', false, text)
await 200
const button = document.querySelector(
'button[data-testid="send-button"]'
)
if (button) {
button.click()
return
} else {
const enterKeyEvent = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
keyCode: 13,
which: 13,
bubbles: true,
cancelable: true,
})
document.dispatchEvent(enterKeyEvent)
}
} catch {
let t = makeToast(
'! Error in prompting automation. Try it again, or report an issue!',
'#ffcccc'
)
await timeout(3000)
t.remove()
return false
}
return true
}
let t = makeToast(
'! Error in prompting automation. Try it again, or report an issue!',
'#ffcccc'
)
await timeout(3000)
t.remove()
return false
} else {
return false
}
}
document.addEventListener('selectionchange', function () {
var selection = window.getSelection().toString().trim()
browser.runtime.sendMessage({
type: 'updateContextMenu',
selection:
selection.substring(0, 10) + (selection.length > 10 ? '...' : ''),
})
})
console.log('content script loaded')