-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
102 lines (83 loc) · 2.45 KB
/
popup.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
var overlay = document.querySelector('.overlay');
/* holds a list of {shortcut, url} objects */
var dataset = new Dataset();
var settings_first_open = true;
function showSavedShortcut(shortcut, url) {
var templ = $('#saved-shortcut-template');
var instance = $(templ).clone();
$(instance).attr('id', 'saved-' + shortcut);
$(instance).find('.shortcut-name').text(shortcut);
$(instance).find('.shortcut-url').text(trim_to_len(url, 150));
$(instance).show();
$(instance).find('button').click(function() {
chrome.storage.sync.remove(shortcut);
$(this).closest('tr').remove();
dataset.removeFromDataset(shortcut);
});
$('#saved-shortcuts-body').prepend(instance);
}
function openSettings() {
$('#settings-menu').show();
if (settings_first_open) {
settings_first_open = false;
chrome.storage.sync.get(function(stored) {
console.log('Stored:', stored);
for (key in stored) {
console.log(key, ' -> ', stored[key]);
showSavedShortcut(key, stored[key]);
}
});
$('#add-shortcut').click(function(ev) {
ev.preventDefault();
var shortcut = $('#link-name').val();
var url = $('#link-url').val();
if (shortcut && shortcut.length > 0) {
chrome.storage.sync.set({'petko': 'minkov'});
var to_store = {}
to_store[shortcut] = url;
chrome.storage.sync.set(to_store, function(args) {
console.log('Saved: ', shortcut, ' ', url);
console.log(arguments);
console.log(args);
showSavedShortcut(shortcut, url);
dataset.addToDataset(shortcut, url);
});
}
});
$('#close-menu').click(function(ev) {
ev.preventDefault();
closeSettingsUI();
});
}
}
$(document).ready(function() {
console.log('Document ready!');
dataset.load(function() {
openSettings();
});
});
function closeSettingsUI() {
console.log('Closing settings UI');
// This goes to background.js and is then transmitted back
// to content.js. popup.js is a part of content.js so content.js
// isn't going to receive its messages.
chrome.runtime.sendMessage('hide_app');
}
$(document).keyup(function(e) {
if (e.which == 27) {
closeSettingsUI();
}
});
overlay.addEventListener('click', function() {
closeSettingsUI();
});
function trim_to_len(url, len) {
if (url.length > len) {
return url.substr(0, len) + '...'
} else {
return url
}
}
function trim(url) {
return trim_to_len(url, 80);
}