forked from zhuowenli/githuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
58 lines (48 loc) · 1.2 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
/*
* @Author: 卓文理
* @Email: [email protected]
* @Date: 2018-01-23 14:26:31
*/
'use strict';
/* eslint-disable */
var form = {};
var $title = $('#title');
var $url = $('#url');
var $img = $('#img');
var $button = $('#button');
var $notification = $('#notification');
chrome.tabs.getSelected(null, function(tab) {
$title.val(tab.title);
$url.val(tab.url);
if (tab.favIconUrl) {
$img.html('<img src="' + tab.favIconUrl + '">')
} else {
$img.html('<text>' + tab.title[0] + '</text>')
}
});
$button.on('click', function() {
var name = $title.val();
var url = $url.val();
if (!name) {
return $('#titleError').show();
}
if (!url) {
return $('#urlError').show();
}
var storage = localStorage.getItem('GITHUBER_BOOKMARKS') || '[]';
var data = JSON.parse(storage);
data.push({
name: name,
logo: $img.find('img') && $img.find('img').attr('src') || '',
url: url,
});
localStorage.setItem('GITHUBER_BOOKMARKS', JSON.stringify(data));
chrome.runtime.sendMessage({
name: 'add',
message: {
id: chrome.runtime.id,
}
}, function(o) {
$notification.addClass('show');
});
});