forked from shatgupt/runmycode-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunmycode.js
More file actions
381 lines (353 loc) · 15.2 KB
/
runmycode.js
File metadata and controls
381 lines (353 loc) · 15.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
'use strict';
((window, document) => {
const rmc = window.runmycode
const $ = rmc.$
const $$ = rmc.$$
const displayLangMap = rmc.displayLangMap
const getFileNameFromElement = rmc.getFileNameFromElement
const getLangFromFileName = rmc.getLangFromFileName
const platforms = rmc.platforms
const platform = rmc.getPlatform()
const body = document.body
let filename, lang, pageConf, runner, runnerCloseBtn, runBtn, runInput, runOutput, codeContainer // this element somewhere contains the code to execute
let codeRunning = false
// find out whether given container has any file of supported language
const hasSupportedLang = (fileNameSelector = pageConf.fileNameSelector, fileContainer = document.body) => {
for (let f of Array.from($$(fileNameSelector, fileContainer))) {
if (getLangFromFileName(getFileNameFromElement(f))) return true
}
return false
}
const injectRunButtons = () => {
$$(pageConf.containerSelector).forEach((fileContainer) => {
const _filename = getFileNameFromElement($(pageConf.fileNameSelector, fileContainer))
if (!getLangFromFileName(_filename)) return // nothing to do if lang not supported
pageConf.injectRunButton($(pageConf.runButtonContainer, fileContainer), _filename)
// Listen to delete of this file to close runner if attached to this file
// Used on Github Gist and Bitbucket Snippets edit pages
if (pageConf.deleteFileSelector) {
$(pageConf.deleteFileSelector, fileContainer).addEventListener('click', () => {
window.dispatchEvent(new CustomEvent('run.button.removed', {detail: fileContainer}))
})
}
})
}
const addRunButtonListeners = (btnContainer = body) => {
$$('.runmycode-popup-runner', btnContainer).forEach((el) => {
el.addEventListener('click', (e) => {
e.preventDefault()
if (codeRunning) {
window.alert(`Already running ${filename}. Wait till it completes.`)
return
}
runner.classList.remove('hidden')
const openRunnerBtn = e.target
if (pageConf.getCodeContainer) {
codeContainer = pageConf.getCodeContainer(openRunnerBtn)
} else {
codeContainer = openRunnerBtn.closest(pageConf.containerSelector)
}
lang = openRunnerBtn.dataset.lang
filename = openRunnerBtn.dataset.filename
runInput.value = ''
runOutput.value = ''
codeRunning = false
runBtn.textContent = 'Run'
runBtn.disabled = false
const langName = displayLangMap[lang]
runBtn.setAttribute('title', `Run ${filename} (${langName})`)
runInput.setAttribute('title', `STDIN to ${filename} (${langName})`)
runInput.setAttribute('placeholder', `STDIN to ${filename} (${langName})`)
runOutput.setAttribute('title', `Output from ${filename} (${langName})`)
runOutput.setAttribute('placeholder', `Output from ${filename} (${langName})`)
})
})
}
const fileWatcher = (fileContainer) => {
let currentFileName, openRunnerBtn
const fileNameInput = $(pageConf.fileNameSelector, fileContainer)
// for Bitbucket, input might not be present by the time this gets called first time
if (fileNameInput) {
fileNameInput.addEventListener('input', () => {
openRunnerBtn = $('.runmycode-popup-runner', fileContainer)
currentFileName = getFileNameFromElement(fileNameInput)
if (getLangFromFileName(currentFileName)) {
// supported lang
if (openRunnerBtn) {
// just update filename, lang if btn already present
openRunnerBtn.dataset.filename = currentFileName
openRunnerBtn.dataset.lang = getLangFromFileName(currentFileName)
window.dispatchEvent(new CustomEvent('run.button.updated', {detail: fileContainer}))
} else {
// add a new Run button
pageConf.injectRunButton($(pageConf.runButtonContainer, fileContainer), currentFileName)
window.dispatchEvent(new CustomEvent('run.button.added', {detail: fileContainer}))
// Listen to delete of this file to close runner if attached to this file
if (pageConf.deleteFileSelector) {
$(pageConf.deleteFileSelector, fileContainer).addEventListener('click', () => {
window.dispatchEvent(new CustomEvent('run.button.removed', {detail: fileContainer}))
})
}
}
} else {
// remove run button, if present, for non-supported lang
if (openRunnerBtn) {
pageConf.removeRunButton(openRunnerBtn)
window.dispatchEvent(new CustomEvent('run.button.removed', {detail: fileContainer}))
}
}
})
}
}
// for places where edit of filename and extension is allowed
// watch for existing files getting renamed on edit or new page
// or watch for new files getting added on edit Gihub Gist or Bitbucket Snippets page
const addFileWatcher = (fileContainer = null) => {
if (fileContainer) {
fileWatcher(fileContainer)
} else {
$$(pageConf.containerSelector).forEach((_fileContainer) => {
fileWatcher(_fileContainer)
})
}
}
const initRunner = () => {
if ($('#runmycode-runner')) return // runner is already present
const runnerWidth = 350
const runnerHTML = `<style>
#runmycode-runner {
width: 350px;
}
</style>
<div id="runmycode-runner" class="hidden">
<div class="panel panel-default">
<div id="runmycode-runner-handle" class="panel-heading">
<button id="runmycode-close-runner" type="button" class="close" title="Shortcut to Close: ESC key">x</button>
<h3 class="panel-title"><a target="_blank" href="https://runmycode.online" title="Go to RunMyCode Online Website">RunMyCode Online</a></h3>
</div>
<div class="panel-body">
<button id="runmycode" type="button" class="btn btn-warning btn-block btn-lg">Run</button>
<div class="panel-group">
<div class="panel panel-default panel-runner">
<div class="panel-heading" title="STDIN to Code">
<h4 class="panel-title">Input</h4>
</div>
<div class="panel-collapse collapse">
<div class="panel-body">
<textarea id="runmycode-run-input" placeholder="STDIN to Code" title="STDIN to Code"></textarea>
</div>
</div>
</div>
<div class="panel panel-default panel-runner">
<div class="panel-heading" title="Output from Code">
<h4 class="panel-title">Output</h4>
</div>
<div id="output-panel" class="panel-collapse collapse in">
<div class="panel-body">
<textarea id="runmycode-run-output" rows="4" placeholder="Output from Code" readonly="true"></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</div>`
// following insertAdjacentHTML should be safe since the above html string is fully static and has no variable/user provided component
body.insertAdjacentHTML('afterbegin', runnerHTML)
/* *** Start Movable popup https://gist.github.com/akirattii/9165836 ****/
runner = $('#runmycode-runner')
runnerCloseBtn = $('#runmycode-close-runner')
runBtn = $('#runmycode')
runInput = $('#runmycode-run-input')
runOutput = $('#runmycode-run-output')
let runnerOffset = { x: 0, y: 0 }
runner.style.left = `${(window.innerWidth - runnerWidth) / 2}px` // have popup in the center of the screen
runnerCloseBtn.addEventListener('click', () => runner.classList.add('hidden'))
window.addEventListener('keydown', (e) => {
if (e.keyCode === 27) runnerCloseBtn.click() // if ESC key pressed
})
const popupMove = (e) => {
runner.style.top = `${e.clientY - runnerOffset.y}px`
runner.style.left = `${e.clientX - runnerOffset.x}px`
}
window.addEventListener('mouseup', () => {
window.removeEventListener('mousemove', popupMove, true)
})
$('#runmycode-runner-handle').addEventListener('mousedown', (e) => {
runnerOffset.x = e.clientX - runner.offsetLeft
runnerOffset.y = e.clientY - runner.offsetTop
window.addEventListener('mousemove', popupMove, true)
e.preventDefault() // disable text selection
})
/* *** End Movable popup ****/
// collapse input, output panels
$$('.panel-runner>.panel-heading').forEach((el) => {
el.addEventListener('click', (ev) => {
ev.target.closest('.panel-heading').nextElementSibling.classList.toggle('in')
})
})
const setRunning = () => {
codeRunning = true
runBtn.textContent = 'Running'
runBtn.disabled = true // disable run button
runOutput.classList.remove('error')
runOutput.value = `Running ${filename} (${displayLangMap[lang]})`
$('#output-panel').classList.add('in')
}
const resetRunning = () => {
codeRunning = false
runBtn.textContent = 'Run'
runBtn.disabled = false // enable run button
}
const callApi = (url, apiKey) => {
const code = pageConf.getCode(codeContainer)
if (code.trim() === '') {
runOutput.classList.add('error')
runOutput.value = 'No code to run!'
resetRunning()
return
}
window.fetch(url, {
method: 'post',
headers: {'x-api-key': apiKey},
body: code
})
.then(res => res.json())
.then((resp) => {
// console.log('Run response', resp)
// do not update runOutput if the attached run btn has changed due to run.button.added event
if (codeRunning) {
runOutput.classList.add('error')
if (resp.status === 'Successful') {
runOutput.classList.remove('error')
runOutput.value = resp.stdout || resp.stderr
} else if (resp.status === 'Failed' || resp.status === 'BadRequest') {
runOutput.value = `Failed: ${resp.error}${resp.stdout}` // stdout for php which puts error in stdout
} else if (resp.message === 'Forbidden') {
runOutput.value = 'Invalid API Key or URL in extension options.\nDefault URL is https://api.runmycode.online/run and your key should be available at https://runmycode.online/dashboard.html after authenticating.'
} else {
runOutput.value = 'Some error happened. Please try again later.' // what else do I know? :/
}
resetRunning()
}
})
.catch((error) => {
console.error('Error:', error)
runOutput.classList.add('error')
runOutput.value = 'Some error happened. Please try again later.' // what else do I know? :/
resetRunning()
})
}
runBtn.addEventListener('click', (e) => {
setRunning()
const getApiUrl = browser.storage.local.get('apiUrl')
const getApiKey = browser.storage.local.get('apiKey')
Promise.all([getApiUrl, getApiKey]).then((result) => {
let apiUrl = result[0]['apiUrl']
const key = result[1]['apiKey']
if (!apiUrl) {
// set default value if not set
apiUrl = 'https://api.runmycode.online/run'
browser.storage.local.set({
'apiUrl': apiUrl
}).then(null, (err) => {
console.error('set apiUrl Error:', err)
})
} else if (!key) {
runOutput.classList.add('error')
runOutput.value = 'Please set the API key in the extension options as generated at https://runmycode.online'
resetRunning()
}
if (apiUrl && key) {
const urlArr = [
`${apiUrl}/${lang}?platform=${platform}`,
`filename=${encodeURIComponent(filename)}`,
`args=${encodeURIComponent(runInput.value)}`
]
callApi(urlArr.join('&'), key)
}
}, (error) => {
console.error('getCreds Error:', error)
runOutput.classList.add('error')
runOutput.value = 'Some error happened. Please try again later.' // what else do I know? :/
resetRunning()
})
})
}
const clearRunner = () => {
if (runner) {
runInput.value = ''
runOutput.value = ''
runnerCloseBtn.click()
}
}
const handlePageUpdate = () => {
// console.log('platform:', platform)
if (platforms[platform]) {
const page = platforms[platform].getPage()
// console.log('page:', page)
clearRunner()
if (page) {
pageConf = platforms[platform]['pages'][page]
// a page can have custom lang check - used at gobyexample.com
if ((pageConf.hasSupportedLang && pageConf.hasSupportedLang()) || hasSupportedLang()) {
// can have custom code to inject run button - used at gobyexample.com
if (pageConf.injectRunButtons) {
pageConf.injectRunButtons()
} else {
injectRunButtons()
}
initRunner()
addRunButtonListeners()
}
// for places where edit of filename and extension is allowed
if (pageConf.addFileWatcher) {
// watch for existing files getting renamed on edit or new page
addFileWatcher()
// watch for new files getting added on edit Gihub Gist or Bitbucket Snippets page
if (pageConf.fileListSelector) {
const fileList = $(pageConf.fileListSelector)
const observer = new MutationObserver((mutations) => {
const newFileContainer = mutations[0].addedNodes[0]
if (newFileContainer) {
addFileWatcher($(pageConf.containerSelector, newFileContainer))
}
})
observer.observe(fileList, { childList: true })
}
}
}
}
}
// this is required because of single page apps like Github, Bitbucket
// where url change and page load complete needs to be detected to init the runner
browser.runtime.onMessage.addListener(msg => {
if (msg === 'pageUpdated') handlePageUpdate()
})
// to forcefully trigger pageUpdate - used in case of Bitbucket edit
window.addEventListener('platform.page.updated', handlePageUpdate, true)
const handleRunButtonRemove = (e) => {
const updatedCodeContainer = e.detail
if (updatedCodeContainer === codeContainer) {
codeRunning = false // to prevent active fetch request to update runner after completion
runnerCloseBtn.click()
}
}
const handleRunButtonUpdate = (e) => {
const updatedCodeContainer = e.detail
if (updatedCodeContainer === codeContainer) {
codeRunning = false // to prevent active fetch request to update runner after completion
// immediately update runner filename, placeholder info only if visible
// if runner is hidden, clicking popup-runner will make it visible
if (!runner.classList.contains('hidden')) $('.runmycode-popup-runner', updatedCodeContainer).click()
}
}
const handleRunButtonAddition = (e) => {
initRunner()
addRunButtonListeners(e.detail || body)
handleRunButtonUpdate(e)
}
window.addEventListener('run.button.added', handleRunButtonAddition, true)
window.addEventListener('run.button.updated', handleRunButtonUpdate, true)
window.addEventListener('run.button.removed', handleRunButtonRemove, true)
})(window, document)