-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
187 lines (174 loc) · 5.47 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
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
182
183
184
185
186
187
var latest_title;
var path;
var pageUrl;
var latestUrl;
var currentTabId;
var nextUrl;
var requested = false;
function initiateVideoHandler(id) {
videoInitiated = true;
chrome.tabs.sendMessage(id, {
action: "startVideoHandler"
}, function (response) {
if (chrome.runtime.lastError) {
console.log("Error in 'startVideoHandler' send: " + chrome.runtime.lastError.message);
} else {
console.log("Succesfully received response: " + response.answer);
}
});
}
function setNext(info, tab) {
if (latest_title === undefined) {
alert("Please set a title first.");
return;
}
currentTabId = tab.id;
nextUrl = info.linkUrl;
pageUrl = info.pageUrl;
var toSave = {};
//Request the path
chrome.tabs.sendMessage(tab.id, {
action: "getPath",
link: nextUrl
}, function (response) {
if (chrome.runtime.lastError) {
console.log("Error in 'getPath' send: " + chrome.runtime.lastError.message);
} else {
console.log("Succesfully received response: " + response.answer);
path = response.answer;
//Save the object
toSave[latest_title] = {
pageUrl: pageUrl,
pathToNext: path
};
chrome.storage.sync.set(toSave, function () {
if (chrome.runtime.lastError) {
console.log("Save failed for item: " + latest_title);
} else {
console.log("Save success! Path had value: " + path);
initiateVideoHandler(tab.id);
}
});
}
});
}
function setEntry(info, tab) {
var title = prompt("Please enter the name of the new title.");
latest_title = title;
var pageUrl = info.pageUrl;
var toSave = {};
currentTabId = tab.id;
toSave[title] = {
pageUrl: pageUrl,
pathToNext: null
};
chrome.storage.sync.set(toSave, function () {
if (chrome.runtime.lastError) {
console.log("Save failed for entry: " + chrome.runtime.lastError.message);
} else {
console.log("Saved entry with title: " + title);
latest_title = title;
latestUrl = pageUrl;
}
});
}
function playNextHandler(link) {
chrome.tabs.update(currentTabId, {
url: link
}, function (tab) {
if (chrome.runtime.lastError) {
console.log("Failed to play next Url: " + nextUrl + ".\n" + chrome.runtime.lastError.message);
} else {
nextUrl = link;
console.log("Succesfully updated tab to 'nextUrl':" + nextUrl);
pageUrl = nextUrl;
}
});
}
function checkLinkForTitle(tab) {
console.log("Check if page is known.");
chrome.storage.sync.get(null, function (items) {
if (chrome.runtime.lastError) {
console.log("Error in item retrieval: " + chrome.runtime.lastError.message);
} else {
console.log("Successfully retrieved items.");
for (var key in items) {
if (items.hasOwnProperty(key)) {
console.log("Checking page:", items[key]["pageUrl"]);
if (items[key]["pageUrl"] === tab.url) {
latestUrl = items[key]["pageUrl"];
latest_title = key;
console.log("Found matching page. Title is: " + latest_title);
currentTabId = tab.id;
return;
}
}
}
console.log("Could not find a matching page.");
}
});
}
function loadTitleInTab(link) {
chrome.tabs.create({
url: link
}, function (tab) {
currentTabId = tab.id;
pageUrl = link;
latestUrl = link;
console.log("Successfully created new tab with url:\n" + tab.url);
});
}
function initiateVideo(tabId) {
console.log("In 'initiateVideo'.");
initiateVideoHandler(tabId);
}
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
console.log("Registered Tab Update: " + changeInfo.status);
if (changeInfo.status === "complete") {
checkLinkForTitle(tab);
if (tabId != currentTabId) return;
initiateVideo(tabId);
}
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
switch (request.action) {
case "playNext":
playNextHandler(request.link);
sendResponse({
answer: latest_title
});
break;
case "updateTitle":
latest_title = request.title;
sendResponse({
answer: latest_title
});
loadTitleInTab(request.link);
break;
case "sendTitle":
isWatchingKnownShow();
sendResponse({
answer: latest_title
});
break;
default:
console.log("Unknown action by content_script.");
sendResponse({
answer: "Ended up in default."
});
break;
}
return true;
});
chrome.contextMenus.create({
id: "New Next",
title: "Set as Next Button",
contexts: ["link"],
onclick: setNext
});
chrome.contextMenus.create({
id: "New Entry",
title: "Title of new entry",
contexts: ["all"],
onclick: setEntry
});