-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratchpad.js
86 lines (55 loc) · 2.4 KB
/
scratchpad.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
// Scratchpad
// ==============================================================
// ==============================================================
// Working example
// http://stackoverflow.com/questions/13872542/chrome-chrome-storage-local-get-and-set
function save() {
var channels = $("#channels").val();
var keywords = $("#keywords").val();
chrome.storage.local.set({'channels': channels});
chrome.storage.local.set({'keywords': keywords});
}
function load() {
var channels = "";
var keywords = "";
chrome.storage.local.get('channels', function (result) {
var channels = result.channels;
alert(result.channels);
$("#channels").val(channels);
});
}
// "Chrome.storage.local.get() returns an object with items in their key-value mappings, so you have to use the index of the key in your search pattern."
// ==============================================================
var derp;
chrome.storage.local.set({"derp" : "B00p"});
chrome.storage.local.get("derp", function(data){
derp = data;
});
console.log("what is derp: " + derp);
// ==============================================================
// Sample get/set usage
chrome.storage.sync.set({ "yourBody": "myBody" }, function(){
// A data saved callback omg so fancy
});
chrome.storage.sync.get(// String or Array //["yourBody"], function(items){
// items = [ { "yourBody": "myBody" } ]
});
// ==============================================================
// Last good working copy before storage feature:
// Toggle script to inject.
var toggleFeedEval = "$('ol.feed.updates').toggle()";
// Inject the script into tab window.
function inject (evalThis) {
chrome.tabs.executeScript(null, {code:evalThis});
};
function injectWithScript () {
inject(toggleFeedEval);
};
// After DOM content loads, add an event listener to the button in popup.html.
// Bind button to toggle behavior.
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('the-button').addEventListener('click', injectWithScript);
});
// ==============================================================
// Lessons learned
// If you try to retrieve a property from chrome.storage.local that isn't set, it won't return underfined. It will return the entire storage object, with all of the prototype methods. You have to check for that property ON the storage object, like result.prop instead of just prop.