Repository navigation
Expand file tree
/
Copy pathcommon-utils.js
More file actions
142 lines (130 loc) · 4.5 KB
/
Copy pathcommon-utils.js
File metadata and controls
142 lines (130 loc) · 4.5 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
'use strict';
((window, document) => {
window.runmycode = {}
const rmc = window.runmycode
rmc.platforms = {}
// Maps supported sites to objects implementing the functionality
// This locationMap is mirrored in background.js.
// Should always be synced to/from there after any change.
const locationMap = {
'github.com': 'github',
'gist.github.com': 'github_gist',
'gitlab.com': 'gitlab',
'gitlab.com/snippets': 'gitlab_snippets',
'bitbucket.org': 'bitbucket',
'bitbucket.org/snippets': 'bitbucket_snippets',
'gobyexample.com': 'gobyexample',
'xahlee.info': 'xahlee',
'www.learntosolveit.com': 'learntosolveit',
'www.geeksforgeeks.org': 'geeksforgeeks',
'www.tutorialspoint.com': 'tutorialspoint'
}
const extMap = {
py: 'python',
py3: 'python3',
js: 'nodejs',
rb: 'ruby',
php: 'php',
java: 'java',
go: 'go',
c: 'c',
cpp: 'cpp',
kt: 'kotlin',
scala: 'scala'
}
// swap key, value from extMap
const langExtMap = Object.assign({}, ...Object.entries(extMap).map(([a, b]) => ({ [b]: a })))
const displayLangMap = {
python: 'Python2',
python3: 'Python3',
nodejs: 'Nodejs',
ruby: 'Ruby',
php: 'PHP',
java: 'Java',
go: 'Go',
c: 'C',
cpp: 'C++',
kotlin: 'Kotlin',
scala: 'Scala'
}
const $ = (s, elem = document) => elem.querySelector(s)
const $$ = (s, elem = document) => elem.querySelectorAll(s)
const getPlatform = () => {
const l = window.location.hostname + '/' + window.location.pathname.split('/')[1]
return locationMap[l] || locationMap[window.location.hostname]
}
const getFileNameFromElement = (elem) => {
if (elem.tagName === 'INPUT') return elem.value.trim()
else return elem.textContent.trim()
}
const getExtFromFileName = (filename) => {
if (filename.indexOf('.') !== -1) return filename.trim().split('.').pop().toLowerCase()
}
const getLangFromFileName = (filename) => extMap[getExtFromFileName(filename)]
// CodeMirror on Bitbucket adds these weird characters
let emptyLineUsedByCodeMirror, nbspUsedByCodeMirror
(() => {
const el = document.createElement('div')
el.innerHTML = '​'
emptyLineUsedByCodeMirror = el.textContent
el.innerHTML = ' '
nbspUsedByCodeMirror = el.textContent
})()
const getCodeFromLines = (lines) => {
return [].map.call(lines, (line) => {
// replace weird characters
const lineText = line.innerText.replace(emptyLineUsedByCodeMirror, '\n').split(nbspUsedByCodeMirror).join(' ')
return lineText === '\n' ? '' : lineText
}).join('\n')
}
// Simple function to create DOM from JSON like args
// Args is an array of 3 items: [tagName, attributes, textContent|child]
// For simplicity, element can have either text or a single direct child
// Example:
// buildDomElement(
// ['div', {'class': 'abc'},
// ['p', {'id': 'show'},
// 'Hello'
// ]
// ]
// )
// Ref1: https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/DOM_Building_and_HTML_Insertion
// Ref2: https://bumbu.github.io/a-safe-function-to-build-html-elements-without-using-innerhtml/
const buildDomElement = (args) => {
const tagName = args[0]
const attrs = args[1]
const textContent = typeof args[2] === 'string' ? args[2] : null
const child = Array.isArray(args[2]) ? args[2] : null
// Create element
const parentNode = document.createElement(tagName.toUpperCase())
// Set attributes
for (let attr in attrs) {
parentNode.setAttribute(attr, attrs[attr])
}
// Add text or DOM child
if (textContent) {
parentNode.appendChild(document.createTextNode(textContent))
} else if (child) {
parentNode.appendChild(buildDomElement(child))
}
return parentNode
}
// Java public classes require filename to be same as class name.
// Try best to extract from source code. Uses the first public class name.
const getJavaPublicClassFileName = (src) => {
const match = (/public class (\w+)/g).exec(src)
if (match !== null) return match[1] + '.java'
}
// export
rmc.$ = $
rmc.$$ = $$
rmc.buildDomElement = buildDomElement
rmc.displayLangMap = displayLangMap
rmc.langExtMap = langExtMap
rmc.getPlatform = getPlatform
rmc.getFileNameFromElement = getFileNameFromElement
rmc.getExtFromFileName = getExtFromFileName
rmc.getLangFromFileName = getLangFromFileName
rmc.getCodeFromLines = getCodeFromLines
rmc.getJavaPublicClassFileName = getJavaPublicClassFileName
})(window, document)