-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
101 lines (88 loc) · 2.48 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
// Omnibox search
// executes a search query against the api and shows
// the list of results in the chrome address bar / dropdown
chrome.omnibox.onInputChanged.addListener(debounce(function(text, suggest) {
authenticate().then(function(token) {
search(text, token).then(function(results) {
if(!results) {
return
}
suggestions = results.map(function(res){
var parser = document.createElement('a')
parser.href = res.url
var domain = parser.hostname
return {
content: res.url,
description: "<url>" + domain + "</url> <dim>" + res.title + "</dim>"
}
})
var len = suggestions.length >= 4 ? 4 : suggestions.length
suggestions = suggestions.slice(0, len)
chrome.storage.sync.get({
server_address: 'http://localhost'
}, function(options) {
suggestions.push({
content: options.server_address,
description: "<url>" + options.server_address + "</url> <dim>Go to app</dim>"
})
suggest(suggestions)
})
})
})
}, 300))
// Handle omnibox search result selection
chrome.omnibox.onInputEntered.addListener(function(url) {
// New or same window should be an option
chrome.storage.sync.get({
link_target: false
}, function(options){
if(options.link_target) {
window.open(url)
return
}
window.location = url
})
})
// Recall specific hotkey / command
chrome.commands.onCommand.addListener(function(command) {
if(command === "bookmark_current_page") {
chrome.tabs.query({active: true}, function(tabs) {
var tab = tabs[0]
fetch_favicon(tab.url).then(function(favicon){
var bookmark = {
url: tab.url,
title: tab.title,
icon: favicon
}
post_bookmark(bookmark)
})
})
}
})
// Hook into chromes built in bookmark mechanism
// and send the recall server when bookmark is added
chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
authenticate().then(function(token){
fetch_favicon(bookmark.url).then(function(base64){
var bm = {
title: bookmark.title,
url: bookmark.url,
icon: base64
}
create_bookmark(bm, token)
})
})
})
function debounce(fn, w) {
var timeout
return function() {
var args = arguments
var ctx = this
var later = function () {
timeout = null
fn.apply(ctx, args)
}
clearTimeout(timeout)
timeout = setTimeout(later, w)
}
}