-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
75 lines (62 loc) · 2.63 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
/*jslint browser: true, devel: true, nomen: true */
(function () {
'use strict';
// https://stackoverflow.com/a/39732154
chrome.runtime.onConnect.addListener(function (externalPort) {
externalPort.onDisconnect.addListener(function () {
// var ignoreError = chrome.runtime.lastError;
// chrome.extension.getBackgroundPage().console.log("onDisconnect");
});
});
// https://stackoverflow.com/a/9310752/2289640
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
function makeQuery(text) {
return '^' + escapeRegExp(text) + '$';
}
chrome.contextMenus.removeAll();
chrome.contextMenus.create({
title: "Show Saved Notes...",
contexts: ["page_action"],
onclick: function () {
chrome.tabs.create({ url: '/notes.html' });
}
});
function notesChangedCallback() {
// Do nothing.
}
var notesStorage = new WebStore(localStorage, 'note', notesChangedCallback),
lastTabId = 0;
function showToolbarButton(tab) {
if (tab) {
lastTabId = tab.id;
chrome.pageAction.show(lastTabId);
if (notesStorage.find("url", makeQuery(tab.url)) !== -1) {
chrome.pageAction.setIcon({ path: "icons.iconarchive.com/icons/fatcow/farm-fresh/16/note-edit-icon.png", tabId: tab.id });
} else {
chrome.pageAction.setIcon({ path: "icons.iconarchive.com/icons/fatcow/farm-fresh/16/note-add-icon.png", tabId: tab.id });
}
}
}
// Fires when the active tab in a window changes.
// Note that the tab's URL may not be set at the time this event fired, but you can listen to onUpdated events to be notified when a URL is set.
// https://developer.chrome.com/extensions/tabs#event-onActivated
chrome.tabs.onActivated.addListener(function (activeInfo) {
setTimeout(function () {
chrome.tabs.get(activeInfo.tabId, function (tab) {
showToolbarButton(tab);
});
}, 200);
});
// Fired when a tab is updated.
// https://developer.chrome.com/extensions/tabs#event-onUpdated
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
showToolbarButton(tab);
});
// Gets all tabs that have the specified properties, or all tabs if no properties are specified.
// https://developer.chrome.com/extensions/tabs#method-query
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
showToolbarButton(tabs[0]);
});
}());