-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
94 lines (77 loc) · 2.23 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
console.log('background.js here');
var dataset = new Dataset();
chrome.runtime.onMessage.addListener(function(message) {
if (message == 'hide_app') {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, message);
}
});
}
if (message == 'refresh_dataset') {
console.log('Refreshing dataset');
dataset.load();
}
});
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, 'show_app');
}
});
});
function xmlEscape(ss) {
return ss.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function match(text) {
substrRegex = new RegExp(text, 'i');
var matches = dataset.getMatches(text);
var results = [];
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
var res = {};
res.content = 'go ' + match.shortcut;
res.description = '';
res.description += '<match>' + xmlEscape(match.shortcut) + '</match> ';
res.description += '<url>' + xmlEscape(match.url) + '</url>';
results.push(res);
}
console.log(results);
if (results.length >= 1) {
chrome.omnibox.setDefaultSuggestion({
description: results[0].description
});
results.shift();
} else {
console.log('new defa');
chrome.omnibox.setDefaultSuggestion({description: 'No shortcut results'});
}
return results;
}
function redirectTo(url) {
if (!url.startsWith('http')) {
url += 'http://';
}
chrome.tabs.update({'url': url});
}
chrome.omnibox.onInputEntered.addListener(function (text, disposition) {
console.log(text, ' ' , disposition);
if (text.startsWith("go ")) {
var shortcut = text.substr(3);
redirectTo(dataset.urlForShortcut(shortcut));
} else {
var matches = dataset.getMatches(text);
redirectTo(matches[0].url);
}
});
chrome.omnibox.onInputChanged.addListener(function (text, suggest) {
console.log('matching', text);
matches = match(text);
if (matches.length > 0) {
suggest(matches);
}
});