-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
95 lines (88 loc) · 3.61 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
'use strict';
/*
savedURLs eg data:
{'https://www.youtube.com/watch?v=NPBCbTZWnq0':{'title':'Yiruma - River Flows in You', 'favicon':'foo'},
'https://www.youtube.com/watch?v=yuboWIBPDvk':{'title':'The Lord of the Rings - Main Theme (Piano Version)', 'favicon':'bar'}}
*/
// these three global funcs should be in a seprate UTILs file...
function saveCurrentURL(){
chrome.tabs.getSelected(null,function(tab) {
var current_url = tab.url;
var current_title = tab.title;
var current_favicon_url = tab.favIconUrl;
// var saved_urls = []; TODO this seemed to not be used anywhere so commented
// TODO(cont) Thought it was for setup initially. test from scratch setup again
// get the object of existing saved URLS
chrome.storage.sync.get({savedURLs: {}}, function(data) {
var savedURLs = data.savedURLs
// append to that object
savedURLs[current_url] = {'title':current_title, 'favicon':current_favicon_url}
// store the list with the newly appended url
chrome.storage.sync.set({savedURLs: savedURLs}, function() {
})
})
});
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function loadPractiseTabs(){
chrome.storage.sync.get({savedURLs: {}}, function(data) {
var allURLsData = data.savedURLs;
// TODO should do something here if no URLs are saved
var allURLs = Object.keys(allURLsData)
chrome.storage.sync.get({defaultTabs: 7}, function(data) {
try{
// first tries this case whereby we are doing ths fnc from the popup
var number_of_tabs_to_open = parseInt(document.getElementById("quantity").value);
}
catch{
// else does this whereby we are doing ths fnc via the keyboard shortcut
number_of_tabs_to_open = data.defaultTabs
}
if (allURLs.length <= number_of_tabs_to_open){
// covers the case where a number higher than the length of the saved array
number_of_tabs_to_open = allURLs.length
}
shuffle(allURLs);
var URLsToOpen = allURLs.slice(0, number_of_tabs_to_open)
for (let item of URLsToOpen) {
chrome.tabs.create({ url: item });
}
});
});
}
//HANDLERS FOR KEYBOARD SHORTCUTS
chrome.commands.onCommand.addListener(function(command) {
if (command === '1-save-tab') {
saveCurrentURL()
//do same as if clicking on the save tab popup btn
} else if (command === '2-open-practise-tabs') {
loadPractiseTabs()
//do same as if clicking on the practise popup btn
}
});
chrome.runtime.onInstalled.addListener(function() {
// chrome.storage.sync.set({savedURLs: []}, function() {
console.log('I am here!!!');
// });
// chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// chrome.declarativeContent.onPageChanged.addRules([{
// conditions: [new chrome.declarativeContent.PageStateMatcher({
// pageUrl: {hostEquals: 'developer.chrome.com'},
// })],
// actions: [new chrome.declarativeContent.ShowPageAction()]
// }]);
// });
});