-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
91 lines (75 loc) · 2.75 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
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
90
91
var user_id;
var always_show;
var feedback_sheet_id = "196vCHcg9O1mR9jYQLeo-jF1PAkbK0VRAHdSlf4ngEZM";
var feedback;
function sendTAFeedback() {
feedback = $("#ta_comments").val();
feedback = feedback.replace(/"/g, '\\"').replace(/'/g, "\\'");
//console.log(feedback);
chrome.identity.getAuthToken({interactive: true}, function(token) {
if (token) {
var xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function () {
if(xhr2.readyState === XMLHttpRequest.DONE && xhr2.status == 200) {
// request done, we sent the data
window.close();
} else if (xhr2.readyState === XMLHttpRequest.DONE) {
console.log(xhr2.responseText);
}
};
xhr2.open("POST",
"https://sheets.googleapis.com/v4/spreadsheets/" + feedback_sheet_id +
"/values/A1:A100000:append?valueInputOption=RAW",
true);
xhr2.setRequestHeader('Authorization','Bearer ' + token);
xhr2.setRequestHeader("Content-type", "application/json");
xhr2.send('{' +
'"range": "A1:A100000",' +
'"values": [[ "' + feedback + '" ]]' +
'}');
} else {
var message = "";
if (chrome.runtime.lastError)
message = chrome.runtime.lastError.message;
console.log("Not signed into Chrome, network error or no permission.\n" + message);
}
});
}
document.addEventListener('DOMContentLoaded', function() {
$("#send_button").click(sendTAFeedback);
// check box if always_show == true
chrome.storage.local.get(null, function(items) {
if (!items.user_id) {
user_id = Math.random().toString(36) + new Date().getTime();
always_show = (Math.random() < 0.5);
console.log("always show: " + always_show);
chrome.storage.local.set({user_id: user_id, always_show: always_show});
} else if (items.always_show == undefined) {
user_id = items.user_id;
always_show = (Math.random() < 0.5);
console.log("always show: " + always_show);
chrome.storage.local.set({always_show: always_show});
} else {
user_id = items.user_id;
always_show = items.always_show;
}
console.log("always show: " + always_show);
if (always_show) {
$("#always_show_checkbox").prop( "checked", true );
}
});
$("#always_show_checkbox").change(function() {
if(this.checked) {
always_show = true;
} else {
always_show = false;
}
chrome.storage.local.set({always_show: always_show});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, "always show changed", function(response) {
console.log("sent always show message: " + response);
});
});
console.log("always show: " + always_show);
});
});