-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
146 lines (111 loc) · 3.54 KB
/
javascript.js
File metadata and controls
146 lines (111 loc) · 3.54 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
//alert("Hello from your Chrome extension!")
var jsonText;
var firebaseConfig = {
apiKey: "AIzaSyCFeWhZ1bEhP83MXFpgSbtA3N7fAVBWPg8",
authDomain: "wiki-new-tab.firebaseapp.com",
databaseURL: "https://wiki-new-tab.firebaseio.com",
projectId: "wiki-new-tab",
storageBucket: "wiki-new-tab.appspot.com",
messagingSenderId: "390798717062",
appId: "1:390798717062:web:b2dc0b3f2e26dbebc0e107",
measurementId: "G-EV7V559H86"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
//console.log("doing good");
var topic = "a";
var ref2 = firebase.database().ref().child(topic);
var arra = [];
var number = Math.floor(Math.random() * 5800);
var currentArticleNo = 1;
loadArticle(number, true);
document.getElementById("rimg").addEventListener("click",function(){
number = Math.floor(Math.random() * 5800);
loadArticle(number, true);
currentArticleNo = arra.length;
updatePrevButton();
});
document.getElementById("previous").addEventListener("click",function(){
currentArticleNo--;
var ano = arra[currentArticleNo-1];
loadArticle(ano, false);
updatePrevButton();
});
function loadArticle(number, shldPush){
if(shldPush == true){
arra.push(number);
}
var ref3 = ref2.child(number);
var obj;
ref3.once("value",function(snapshot){
obj = JSON.parse(JSON.stringify(snapshot.val()));
/*var number = Math.floor(Math.random() * 5800);
if(number == 0){number = 1;}*/
var text = obj.text;
var href = obj.href;
//console.log("going good");
//console.log(text);
var title = document.getElementById("link");
//console.log(title.innerHTML);
title.innerHTML = text;
recreateNode(title, true);
title = document.getElementById("link");
title.addEventListener("click",function(){
window.open("https://www.wikipedia.org"+href);
});
//window.open("https://www.wikipedia.org"+href,"_self");
});
console.log("article no. = "+currentArticleNo);
/*recreateNode(document.getElementById("previous"), true);
*/
}
var cors_api_url = 'https://cors-anywhere.herokuapp.com/';
jsonText = doCORSRequest({
method: 'GET'
}, function printResult(result) {
//console.log(result);
});
function doCORSRequest(options, printResult) {
var x = new XMLHttpRequest();
x.open(options.method, cors_api_url + "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=5");
x.onload = x.onerror = function() {
setUrlFromJson((x.responseText || ''));
printResult(
/*options.method + ' ' + "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" + '\n' +
x.status + ' ' + x.statusText + '\n\n' +*/
(x.responseText || '')
);
};
if (/^POST/i.test(options.method)) {
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
x.send("");
}
function setUrlFromJson(jsonT){
//console.log(jsonT);
var jsonText = JSON.parse(jsonT);
var url = "https://www.bing.com" + jsonText.images[0].url;
//console.log(url);
/*document.body.classList.add("bg");*/
/*document.body.style.backgroundImage = "url("+ url+")";*/
var bg = document.getElementById("bg");
bg.src = url;
}
function recreateNode(el, withChildren) {
if (withChildren) {
el.parentNode.replaceChild(el.cloneNode(true), el);
}
else {
var newEl = el.cloneNode(false);
while (el.hasChildNodes()) newEl.appendChild(el.firstChild);
el.parentNode.replaceChild(newEl, el);
}
}
function updatePrevButton(){
if(currentArticleNo == 1){
document.getElementById("prev").style.display = "none";
}else{
document.getElementById("prev").style.display = "inline-block";
}
}