-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
77 lines (66 loc) · 2.74 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
// Copyright (c) 2012, Derek Guenther
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Saves options to Chrome Sync.
function save_options() {
var key_field = document.getElementById("api_key");
var api_key = key_field.value;
// Clear the storage so we don't persist old data.
chrome.storage.sync.remove("api_key", function() {
// Clear our local cached data
chrome.storage.local.clear();
// Test out the new api key.
var xhr = new XMLHttpRequest();
xhr.onload = function () {
var json = xhr.responseText;
json = JSON.parse(json);
if (json.error) {
// If there's an error, update the badge.
chrome.browserAction.setBadgeText({text:"!"});
// Also, notify the user.
var status = document.getElementById("status");
status.innerHTML = "Sorry, that API key isn't valid. Please try again!";
setTimeout(function() {
status.innerHTML = "";
}, 4000);
} else {
// Store the api key in Chrome Sync.
chrome.storage.sync.set({"api_key": api_key}, function() {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.innerHTML = "Your options have been saved. Thanks, " + String(json.user_information.username) + "!";
setTimeout(function() {
status.innerHTML = "";
}, 4000);
});
}
};
var url = "http://www.wanikani.com/api/v1.1/user/" + encodeURIComponent(api_key) + "/study-queue";
xhr.open("GET", url);
xhr.send();
});
}
// Restore all options to their form elements.
function restore_options() {
restore_api_key();
}
// Restore API key text box.
function restore_api_key() {
chrome.storage.sync.get("api_key", function(data) {
var api_key = data.api_key;
// If no API key is stored, leave the text box blank.
// We don't set a default value for the API key because it must be set
// for the extension to work.
if (!api_key) {
return;
}
var key_field = document.getElementById("api_key");
key_field.value = api_key;
});
}
function bind_save() {
document.querySelector('#save').addEventListener('click', save_options);
}
document.addEventListener('DOMContentLoaded', restore_options);
document.addEventListener('DOMContentLoaded', bind_save);