-
Notifications
You must be signed in to change notification settings - Fork 11
/
options.js
89 lines (75 loc) · 2.6 KB
/
options.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
var CONTEXT_MENU_CACHES = [
"google-cache",
"wayback-machine",
"coral-cdn"
];
// Remove context menu caches
function removeContextMenu(cache) {
var saveObj = {};
saveObj[cache] = false;
chrome.contextMenus.remove(cache);
chrome.storage.sync.set(saveObj);
}
// Create context menu caches
function createContextMenu(cache) {
var saveObj = {};
var create_properties = {
id: cache,
title: "Open page with " + cache.split("-").join(" "), //$("label[for=""+ cache +""]").text()
contexts: ["page"]
};
saveObj[cache] = true;
chrome.contextMenus.create(create_properties);
chrome.storage.sync.set(saveObj);
}
// Add/Remove context menu caches
function updateContextMenuCaches(event) {
var cache = $(event.target).attr("for");
var add_context_menu = !$("#" + cache).is(":checked");
add_context_menu ? createContextMenu(cache) : removeContextMenu(cache);
}
var order = [];
$("#sortable").sortable({
stop: function(event, ui) { // Save settings after change
order.splice(order.indexOf(ui.item.attr("id")), 1);
order.splice(ui.item.index(), 0, ui.item.attr("id"));
var saveObj = {};
saveObj["cacheOrder4"] = order;
chrome.storage.sync.set(saveObj, function() {
console.log("Saved Cache Ordering Preferences");
});
},
// Set up sortable, checkboxes, & toggle
create: function(event, ui) {
// Set up sortable
chrome.storage.sync.get("cacheOrder4", function(result) {
var ul = $("#sortable");
var li = ul.children("li").get();
order = result["cacheOrder4"] || ["google-cache-sortable", "wayback-machine-sortable", "coral-cdn-sortable"];
li.sort(function(a, b) {
return order.indexOf($(a).attr("id")) - order.indexOf($(b).attr("id"));
});
ul.append(li);
});
// Set up auto-detect toggle
chrome.storage.sync.get("auto-detect", function(result) {
var toggle = $("#myonoffswitch");
toggle.prop("checked", result["auto-detect"]);
});
// Set up context menu caches
chrome.storage.sync.get(CONTEXT_MENU_CACHES, function(results) {
Object.keys(results).forEach(function(key) {
$("#" + key).prop("checked", !!results[key]);
});
});
}
});
// Save auto-detect settings
$("#myonoffswitch").click(function(event) {
chrome.storage.sync.set({"auto-detect": $("#myonoffswitch").is(":checked")}, function() {
console.log("Saved Auto-Detect Preferences");
});
});
// Add/Remove context menu caches
$(".context-menu-label").click(updateContextMenuCaches);
$("#sortable").disableSelection();