This repository has been archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
75 lines (60 loc) · 2.67 KB
/
content.js
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
// abandon all hope ye who enter here
(function() {
var doc = document;
// jsfiddle input, div, btn to target
var jsfiddle = {
input: doc.getElementById("external_resource"),
add_btn: doc.querySelector(".bt-plus-circle"),
target_div: doc.querySelectorAll(".ebCont")[1]
}
// extension url and div to create
var ext = {
cdn_url: "https://gist.githubusercontent.com/samayo/f57a51f319b793ac5bd19325affb3b3b/raw/5aeacf673b0f99c1c1b16d0f46fca126e765b68b/gistfile1.txt",
wrapper_div: function(){
return doc.querySelector(".jsfiddle_cdn_loader");
},
create_wrapper_div: function() {
return doc.createElement("div");
}
}
var xmlhttp = new XMLHttpRequest();
// on keyup send ajax request to get list of cdn
jsfiddle.input.addEventListener('keyup', function(e) {
// clear input field if text is deleted (ex: backspace)
if (jsfiddle.input.value.length <= 1) {
if (doc.body.contains(ext.wrapper_div())) {
ext.wrapper_div().innerHTML = ''
return ;
}
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var el = ext.create_wrapper_div()
el.className = "jsfiddle_cdn_loader"
var json = JSON.parse(xmlhttp.responseText)
var libraries = json.results.sort();
var element_ul = doc.createElement("ul");
for (i = 0; i < libraries.length; i++) {
if (libraries[i].name.startsWith(jsfiddle.input.value)) {
var element_li = doc.createElement('li'),
element_a = doc.createElement("a");
element_a.innerText = libraries[i].name;
element_a.setAttribute("href", "#");
element_a.dataset.cdn = libraries[i].latest;
element_li.appendChild(element_a);
element_ul.appendChild(element_li);
}
el.appendChild(element_ul);
jsfiddle.target_div.appendChild(el)
}
// if library is selected from list, clear the dropdown
element_ul.addEventListener("click", function(e) {
jsfiddle.input.value = e.target.dataset.cdn;
el.innerHTML = ''
})
}
}
xmlhttp.open("GET", ext.cdn_url, true)
xmlhttp.send(null)
});
})();