forked from efexen/elixir-tab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelixir_tab.js
170 lines (134 loc) · 4.14 KB
/
elixir_tab.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
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
var ElixirTab = function() {
var baseUrl = "https://hexdocs.pm/elixir/";
function fetchResource(url, callback) {
window.fetch(url).then(function(response) {
return response.text();
}).then(function(data) {
callback(null, data);
}).catch(handleError);
}
function handleError(err) {
var container = createContainer();
var errorTitle = document.createElement("h2");
errorTitle.innerText = "Oh no something went wrong getting the docs :(";
errorTitle.className = "error-title";
console.log(err);
container.append(errorTitle);
document.body.append(container);
}
function fetchPage(url, callback) {
var page = document.createElement("html");
fetchResource(url, function(err, data) {
page.innerHTML = data;
callback(page);
});
}
function expired(timestamp) {
return timestamp < (Date.now() - 1000 * 60 * 60 * 24);
}
function cacheModules(modules) {
chrome.storage.local.set({
timestamp: Date.now(),
modules: modules
});
}
function fetchModules(callback) {
chrome.storage.local.get(function(cache) {
if (cache.modules && !expired(cache.timestamp)) {
return callback(cache.modules);
}
fetchPage(baseUrl + "Kernel.html", function(basePage) {
var sidescript = basePage.querySelector("head script[src*=sidebar_items]");
var side_url = baseUrl + sidescript.attributes["src"].value;
fetchResource(side_url, function(err, data) {
var sideitems = data.slice(13);
var modules = JSON.parse(sideitems).modules;
cacheModules(modules);
callback(modules);
});
});
});
}
function selectRandomElement(array) {
var index = Math.ceil(Math.random() * array.length);
return array[index];
}
function onlyFunctionKeys(group) {
return group.key === "functions";
}
function findRandomModuleFunction(modules) {
var fun;
var functionEntries = [];
var module;
var counter = 0;
while (!fun && counter < 50) {
module = selectRandomElement(modules);
if (module && module.nodeGroups) {
functionEntries = module.nodeGroups.filter(onlyFunctionKeys);
}
if (module && functionEntries.length > 0) {
fun = selectRandomElement(functionEntries[0].nodes);
}
counter++;
}
renderFunction({
module: module,
fun: fun
});
}
function fetchFunctionDoc(modulefun, callback) {
var pageurl = baseUrl + modulefun.module.id + ".html";
fetchPage(pageurl, function(docpage) {
var selector = modulefun.fun.anchor
.replace("/", '\\\/')
.replace("?", '\\\?')
.replace("!", '\\\!');
return callback(docpage.querySelector("#" + selector));
});
}
function createModuleTitle(module) {
var title = document.createElement("h1");
title.innerHTML = module.title;
return title;
}
function createFunctionTitle(fun) {
var title = document.createElement("h2");
title.className = "function-title";
title.innerHTML = fun.id;
return title;
}
function createContainer() {
var container = document.createElement("div");
container.className = "main";
return container;
}
function createHexdocsLink(modulefun) {
var link = document.createElement("a");
link.innerText = "Open in hexdocs.pm";
link.href = baseUrl + modulefun.module.id + ".html#" + modulefun.fun.anchor;
link.className = "hexdocs-link";
return link;
}
function renderFunction(modulefun) {
fetchFunctionDoc(modulefun, function(docs) {
var moduleTitle = createModuleTitle(modulefun.module);
var functionTitle = createFunctionTitle(modulefun.fun);
var hexdocsLink = createHexdocsLink(modulefun);
var container = createContainer();
container.append(moduleTitle);
container.append(functionTitle);
container.append(docs);
container.append(hexdocsLink);
document.body.append(container);
});
}
function init() {
fetchModules(findRandomModuleFunction);
};
return {
init: init
};
}();
document.addEventListener("DOMContentLoaded", function() {
ElixirTab.init();
});