forked from taicki/omnibox-timer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chrome.js
100 lines (86 loc) · 2.5 KB
/
chrome.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
// Store thistory
var thistory = timerHistory();
resetDefaultSuggestion();
chrome.omnibox.onInputEntered.addListener(function(text){
console.log(thistory);
if (text == "options" || text == "show") {
openOptionsPage();
} else if (text == "clr"){
clearAllNotifications();
} else {
var result = tryToSetupTimer(text);
// Store thistory when a timer is set
if (result) {
thistory.add(text);
}
}
});
function updateDefaultSuggestion(text) {
if (text.trim() === "") {
resetDefaultSuggestion();
} else {
var arr = text.split(/\s+/);
var seconds = parseTime(arr.shift());
if (!seconds) {
console.log("parse error: " + text);
}
if (arr.length > 0) {
desc = arr.join(" ");
} else {
desc = 'Timer done!';
}
var timer = {
currentTime: (new Date()).getTime(),
desc: desc,
seconds: seconds,
popup: 0
};
var notificationTime = timer.currentTime + timer.seconds * 1000;
chrome.omnibox.setDefaultSuggestion({
description: 'Timer set: <match>' + text + '</match> | <time> [<message>] | ' + 'Will alert in ' + moment(notificationTime).calendar()
});
}
}
function resetDefaultSuggestion() {
chrome.omnibox.setDefaultSuggestion({
description: 'Timer set: <time> [<message>]'
});
}
chrome.omnibox.onInputStarted.addListener(function() {
resetDefaultSuggestion();
});
chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
updateDefaultSuggestion(text);
chrome.storage.local.get({historySuggestionType: "time"}, function(object) {
var suggestions = [];
if (object.historySuggestionType === "time") {
var founds = thistory.findByTime(text);
} else {
var founds = thistory.findByCount(text);
}
for (var i = 0; i < founds.length; i++) {
var found = founds[i];
suggestions.push({
content: found["text"],
description: found["text"] + " - <dim>Used " + found["count"] + " time(s)</dim>"
});
}
suggest(suggestions);
});
});
chrome.omnibox.onInputCancelled.addListener(function(text, suggest) {
resetDefaultSuggestion();
});
chrome.action.onClicked.addListener(function(tab) {
openOptionsPage();
});
function openOptionsPage() {
var url = chrome.runtime.getURL("options.html");
chrome.tabs.query({url: url, currentWindow: true}, function(tabs) {
if (tabs.length > 0) {
chrome.tabs.update(tabs[0].id, {active: true});
} else {
chrome.tabs.create({url: url});
}
});
}