-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
50 lines (47 loc) · 1.59 KB
/
background.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
// Scrolls page automatically for upto 25 seconds
function pageScroll() {
var startTime = new Date().getTime();
var interval = setInterval(function () {
if (new Date().getTime() - startTime > 25000) {
clearInterval(interval);
buttonClicked();
return;
}
window.scrollBy(0, 10000);
}, 2000);
}
var titles = [];
var fileDOM = [];
function buttonClicked() {
// Extracts the URLs for all the elements with the given class name having all the documents
var anchorTags = document.getElementsByTagName("a");
var hrefs = [];
for (var i = 0; i < anchorTags.length; i++) {
var hrefString = anchorTags[i].href;
if (hrefString.includes("drive.google.com") && !hrefString.includes("drive.google.com/drive/folders/")) {
if (!hrefs.includes(hrefString)) {
hrefs.push(hrefString);
var titleString = anchorTags[i].textContent;
titles.push(titleString);
}
else if(hrefs.includes(hrefString)){
fileDOM.push(anchorTags[i]);
}
}
}
var titleLinks = { hrefList: hrefs, titleList: titles };
// Send the javascript object of title of document and link of document to the extension javascript file
chrome.runtime.sendMessage(titleLinks);
}
// Receives request from content.js to scroll a document into view
chrome.runtime.onMessage.addListener(function (response, sender, sendResponse) {
var elementIndex = titles.indexOf(response);
var element = fileDOM[elementIndex];
element.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "nearest",
});
});
// Function call to initiate auto page scrolling
pageScroll();