Greasemonkey: various javascript fixups to GM wrapper template.

Thanks to @sandrosc. A few breaking changes fixed (default method to
GM_xhr not working, GM_listvalues not cleaning up output, GM_setvalue
param checking logic wrong) and a few hygenic changes made.
This commit is contained in:
Jimmy 2017-10-10 20:54:58 +13:00
parent c0832eb04b
commit 7c497427ce

View File

@ -20,11 +20,9 @@
}
function GM_setValue(key, value) {
if (typeof key !== "string") {
throw new Error("GM_setValue requires the first parameter to be of type string, not '"+typeof key+"'");
}
if (typeof value !== "string" ||
typeof value !== "number" ||
checkKey(key, "GM_setValue");
if (typeof value !== "string" &&
typeof value !== "number" &&
typeof value !== "boolean") {
throw new Error("GM_setValue requires the second parameter to be of type string, number or boolean, not '"+typeof value+"'");
}
@ -32,24 +30,20 @@
}
function GM_getValue(key, default_) {
if (typeof key !== "string") {
throw new Error("GM_getValue requires the first parameter to be of type string, not '"+typeof key+"'");
}
checkKey(key, "GM_getValue");
return localStorage.getItem(_qute_script_id + key) || default_;
}
function GM_deleteValue(key) {
if (typeof key !== "string") {
throw new Error("GM_deleteValue requires the first parameter to be of type string, not '"+typeof key+"'");
}
checkKey(key, "GM_deleteValue");
localStorage.removeItem(_qute_script_id + key);
}
function GM_listValues() {
var i, keys = [];
for (i = 0; i < localStorage.length; i = i + 1) {
var keys = [];
for (var i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).startsWith(_qute_script_id)) {
keys.push(localStorage.key(i));
keys.push(localStorage.key(i).slice(_qute_script_id.length));
}
}
return keys;
@ -62,7 +56,7 @@
// Almost verbatim copy from Eric
function GM_xmlhttpRequest(/* object */ details) {
details.method = details.method.toUpperCase() || "GET";
details.method = details.method ? details.method.toUpperCase() : "GET";
if (!details.url) {
throw ("GM_xmlhttpRequest requires an URL.");
@ -99,26 +93,24 @@
}
function GM_addStyle(/* String */ styles) {
var oStyle = document.createElement("style");
oStyle.setAttribute("type", "text/css");
oStyle.appendChild(document.createTextNode(styles));
var head = document.getElementsByTagName("head")[0];
if (head === undefined) {
document.onreadystatechange = function () {
if (document.readyState == "interactive") {
var oStyle = document.createElement("style");
oStyle.setAttribute("type", "text/css");
oStyle.appendChild(document.createTextNode(styles));
document.getElementsByTagName("head")[0].appendChild(oStyle);
}
}
}
else {
var oStyle = document.createElement("style");
oStyle.setAttribute("type", "text/css");
oStyle.appendChild(document.createTextNode(styles));
head.appendChild(oStyle);
}
}
unsafeWindow = window;
var unsafeWindow = window;
//====== The actual user script source ======//
{{ scriptSource }}