forked from ForestJohnson/TurtleJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdnSearch.js
181 lines (164 loc) · 5.81 KB
/
mdnSearch.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
171
172
173
174
175
176
177
178
179
180
181
(function(__tjs){
var googleAPIKey = 'AIzaSyCi7M1L69QPOjeOzqs93i6D07957gUPzy0';
var googleMDNSearch = '004844842177012604701:qpwcasb2_dy';
var customSearch =
'https://www.googleapis.com/customsearch/v1?key='
+googleAPIKey+'&cx='+googleMDNSearch;
var searchCachePrefix = '__tjs.mdnSearch.';
var navigationStack = [];
__tjs.mdnSearchPopup = function (q) {
if(q.replace(/[^a-zA-Z]/g,'') !== '') {
var cachedUrl = localStorage[searchCachePrefix+q];
var cachedTitle = localStorage[searchCachePrefix+'.title.'+q];
if(cachedUrl) {
openSearchResultModal(cachedUrl, cachedTitle, q);
} else {
xhrGET(customSearch+'&q='+q).then(function(response){
var googleSearchResults = JSON.parse(response.responseText);
if(googleSearchResults.items && googleSearchResults.items[0]) {
var url = googleSearchResults.items[0].link;
var pageTitle = googleSearchResults.items[0].title;
localStorage[searchCachePrefix+q] = url;
localStorage[searchCachePrefix+'.title.'+q] = pageTitle;
openSearchResultModal(url, pageTitle, q);
}
},
function(response) {
console.error(response);
});
}
}
}
function navigate (url) {
if(url.indexOf("http") != 0) {
url = `https://developer.mozilla.org${url}`
}
document.getElementById('__mdnIframeBackButton').disabled = false;
document.getElementById('__mdnIframeBackButton').onclick = () => {
navigationStack.pop()
openSearchResultModal(navigationStack[navigationStack.length-1], null, null, true);
document.getElementById('__mdnIframeBackButton').disabled = navigationStack.length <= 1;
}
openSearchResultModal(url, null, null)
}
function openSearchResultModal(url, pageTitle, searchText, isBackButton) {
var iframe = document.getElementById('__mdnIframe');
var container = document.getElementById('__mdnIframeContainer');
container.style.display = 'block';
if(!isBackButton) {
navigationStack.push(url);
}
xhrGET(url).then(
function(response) {
iframe.contentWindow.document.open();
iframe.contentWindow.__tjsNavigate = navigate;
iframe.contentWindow.document.write(proxify(response.responseText));
iframe.contentWindow.document.close();
},
function(response) {
console.error(response);
}
);
document.getElementById('__mdnIframeUrlBar').value = url;
if(pageTitle != null && pageTitle != '' && searchText != null && searchText != '') {
var title = `Top result for google search: \"site:developer.mozilla.org ${searchText}\": ${pageTitle} `;
document.getElementById('__mdnIframeTitle').innerText = title;
}
}
function proxify(htmlText) {
const INITIAL_STATE = 0;
const TAG_STATE = 1;
const PROPERTY_NAME_STATE = 2;
const PROPERTY_VALUE_STATE = 3;
var state = INITIAL_STATE;
var insideString = "";
var previousCharacterWasEscapeCharacter = false;
var tag = "";
var propertyName = "";
var propertyValue = "";
var outputHtmlText = "";
for(var i = 0; i < htmlText.length; i++) {
var character = htmlText[i];
if(state == INITIAL_STATE) {
if(character == "<") {
state = TAG_STATE;
}
outputHtmlText += character;
} else if(state == TAG_STATE) {
if(character == ">") {
tag = "";
state = INITIAL_STATE;
} else if(character != " " && character != "\t") {
tag += character;
} else if(tag.length != 0) {
state = PROPERTY_NAME_STATE;
}
outputHtmlText += character;
} else if(state == PROPERTY_NAME_STATE) {
if(character != " " && character != "\t" && character != "=") {
propertyName += character;
}
if(character == "=") {
state = PROPERTY_VALUE_STATE;
}
if(character == ">") {
tag = "";
propertyName = "";
state = INITIAL_STATE;
}
if((character == " " || character == "\t") && propertyName.length > 0) {
propertyName = "";
}
outputHtmlText += character;
} else if(state == PROPERTY_VALUE_STATE) {
if(insideString == "") {
if(character == "'" || character == "\"") {
insideString = character;
}
outputHtmlText += character;
} else {
if(character == "\\") {
previousCharacterWasEscapeCharacter = true;
propertyValue += character;
} else if(character == insideString && !previousCharacterWasEscapeCharacter) {
if(tag == "a" && propertyName == "href") {
outputHtmlText += `#${propertyValue}${insideString} onclick="__tjsNavigate('${propertyValue}')"`;
} else {
outputHtmlText += `${propertyValue}${insideString}`;
}
insideString = "";
propertyValue = "";
propertyName = "";
state = PROPERTY_NAME_STATE;
} else {
propertyValue += character;
}
}
}
}
return outputHtmlText;
}
function xhrGET (url) {
return new Promise(function (resolve, reject){
var request = new XMLHttpRequest();
request.addEventListener('load', resolveFromRequest);
request.addEventListener("error", rejectFromRequest);
request.addEventListener("abort", rejectFromRequest);
request.open("get", url, true);
request.send();
function resolveFromRequest ()
{
resolve({
status: this.status,
responseText: this.responseText
});
}
function rejectFromRequest () {
reject({
status: this.status,
statusText: this.statusText
});
}
});
}
})(__tjs);