-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
146 lines (136 loc) · 4.12 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
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
const API_KEY = config.API_KEY;
const SLIDES_DISCOVERY_DOCS = [
"https://slides.googleapis.com/$discovery/rest?version=v1",
];
const DISCOVERY_DOCS = [
"https://sheets.googleapis.com/$discovery/rest?version=v4",
];
var scontent = new Array();
function onGAPILoad() {
gapi.client
.init({
// Don't pass client nor scope as these will init auth2, which we don't want
apiKey: API_KEY,
discoveryDocs: SLIDES_DISCOVERY_DOCS,
})
.then(
function () {
console.log("gapi initialized");
chrome.identity.getAuthToken({ interactive: true }, function (token) {
gapi.auth.setToken({
access_token: token,
});
});
},
function (error) {
console.log("error", error);
}
);
}
var currPresId = null;
// when url changes checks if presentation is same, if not get's info
function getPresData(pId) {
if(currPresId != pId) {
currPresId = pId;
gapi.client.slides.presentations.get({
presentationId: pId,
}).then(function (response) {
console.log(response.result.title);
console.log(response.result.slides.length);
getSlides(pId);
})
}
else {
console.log("presentation already loaded");
}
}
// gets the id of each slide and prints to console, only used for new presentations
function getSlides(pId) {
gapi.client.slides.presentations.get({
presentationId: pId,
}).then(function (response) {
response.result.slides.forEach((slide) => {
console.log(slide.objectId);
var id = slide.objectId;
var trigger = "next";
var search_value = {};
search_value[id] = trigger
chrome.storage.local.get(search_value, function(result){
var storage_value = {};
storage_value[id] = result[id];
chrome.storage.local.set(
storage_value,
()=>{console.log("Storing trigger " + trigger + " for " + slide.objectId)
});
});
})
})
}
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
// read changeInfo data and do something with it
// like send the new url to contentscripts.js
if (changeInfo.url) {
chrome.tabs.sendMessage( tabId, {
message: 'urlChange',
url: changeInfo.url
})
var urlInfo = changeInfo.url.split("/");
console.log(urlInfo[5]);
getPresData(urlInfo[5]);
// var slideInfo = urlInfo[6].split(".");
// getLastWord(urlInfo[5], slideInfo[1]);
// console.log(slideInfo[1]);
}
}
);
function getLastWord(pId, sId) {
var wordElements;
gapi.client.slides.presentations.get({
presentationId: pId,
pageObjectId: sId,
}).then(function (response) {
response.pageElements.forEach((pageElement) => {
if(pageElement.shape != undefined &&
pageElement.shape.shapeType === "TEXT_BOX") {
pageElement.shape.text.textElements.forEach((element) => {
if(element.textRun != undefined &&
element.textRun.content != " " &&
element.textRun.content != "") {
wordElements.push(element.textRun.content);
}
})
}
})
})
console.log(wordElements);
}
function getWord(pId,sId) {
let textOnSlide = new Array;
gapi.client.slides.presentations.get({
presentationId: pId,
}).then(function (response) {
response.result.slides.forEach((slide) => {
if(slide.objectId === sId) {
slide.pageElements.forEach((pageElement) => {
if(pageElement.shape != undefined && pageElement.shape.shapeType === "TEXT_BOX") {
pageElement.shape.text.textElements.forEach((textElement) => {
if(textElement.textRun != undefined) {
textOnSlide.push(textElement.textRun.content.replace(/[^a-zA-Z ]/g, ""));
var tst = textElement.textRun.content.replace(/[^a-zA-Z ]/g, "").split(" ");
textOnSlide.push(tst[tst.length - 1]);
}
})
}
})
}
})
var lastWord = textOnSlide[textOnSlide.length - 1];
if(lastWord != null) {
console.log(lastWord.toLowerCase());
}
else {
console.log("next");
}
})
}