mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-03 19:56:22 -07:00
Use different functions for cookie return types
This commit is contained in:
parent
744e915d51
commit
cc9d0ed828
6 changed files with 20 additions and 67 deletions
|
@ -3,8 +3,8 @@ import $ from "jquery";
|
||||||
/**
|
/**
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
*/
|
*/
|
||||||
export function getCookie(name, options = {type: "string"}) {
|
export function getCookie(name, defaultVal = "") {
|
||||||
let val = options.default;
|
let val = defaultVal;
|
||||||
let cookieArr = document.cookie.split("; ");
|
let cookieArr = document.cookie.split("; ");
|
||||||
|
|
||||||
for(const cookie of cookieArr) {
|
for(const cookie of cookieArr) {
|
||||||
|
@ -13,29 +13,20 @@ export function getCookie(name, options = {type: "string"}) {
|
||||||
try {
|
try {
|
||||||
val = decodeURIComponent(pair[1]);
|
val = decodeURIComponent(pair[1]);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
return options.default;
|
return defaultVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch(options.type) {
|
|
||||||
case "int":
|
|
||||||
return parseInt(val);
|
|
||||||
case "float":
|
|
||||||
return parseFloat(val);
|
|
||||||
case "bool":
|
|
||||||
case "boolean":
|
|
||||||
return val == "true" || val == "1";
|
|
||||||
case "json":
|
|
||||||
try {
|
|
||||||
return JSON.parse(val);
|
|
||||||
} catch(e) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(val == undefined)
|
|
||||||
val = "";
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getNumberCookie(name, defaultVal = "0") {
|
||||||
|
return parseFloat(getCookie(name, defaultVal))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getBooleanCookie(name, defaultVal = "true") {
|
||||||
|
return getCookie(name, defaultVal) == "true";
|
||||||
|
}
|
||||||
|
|
||||||
function randomPassword(len = 8) {
|
function randomPassword(len = 8) {
|
||||||
const printableStart = 33;
|
const printableStart = 33;
|
||||||
const printableEnd = 126;
|
const printableEnd = 126;
|
||||||
|
|
|
@ -173,41 +173,3 @@ export function createStaffMenu(rank = staffInfo.Rank) {
|
||||||
$topbar.after($staffMenu);
|
$topbar.after($staffMenu);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a lightbox for using staff tools without having to go load the page
|
|
||||||
* @param {string} actionURL The URL to get the action HTML from
|
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
export function openStaffLightBox(actionURL) {
|
|
||||||
$.ajax({
|
|
||||||
method: 'GET',
|
|
||||||
url: `${webroot}manage`,
|
|
||||||
data: {
|
|
||||||
action: actionURL,
|
|
||||||
},
|
|
||||||
dataType:"html",
|
|
||||||
async:false,
|
|
||||||
|
|
||||||
success: result => {
|
|
||||||
let body = `<div id="body-mock">${result.replace(/^[\s\S]*<body.*?>|<\/body>[\s\S]*$/ig, "")}</div>`;
|
|
||||||
let $body = $(body);
|
|
||||||
let header = $body.find("h1");
|
|
||||||
let headerText = header.text();
|
|
||||||
header.remove();
|
|
||||||
if(headerText == "") headerText = "Manage";
|
|
||||||
showLightBox(headerText,$body.html());
|
|
||||||
},
|
|
||||||
error: result => {
|
|
||||||
let responseText = result.responseText;
|
|
||||||
header = responseText.substring(responseText.indexOf("<h1>")+4,responseText.indexOf("</h1>"));
|
|
||||||
|
|
||||||
responseText = responseText.substring(responseText.indexOf("</h1>") + 5, responseText.indexOf("</body>"));
|
|
||||||
if(header == "") {
|
|
||||||
showLightBox("Manage", responseText);
|
|
||||||
} else {
|
|
||||||
showLightBox(header, responseText);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -190,7 +190,10 @@ export function reportPost(id, board) {
|
||||||
};
|
};
|
||||||
xhrFields[`check${id}`] = "on";
|
xhrFields[`check${id}`] = "on";
|
||||||
$.post(webroot + "util", xhrFields).fail(data => {
|
$.post(webroot + "util", xhrFields).fail(data => {
|
||||||
alertLightbox(`Report failed: ${data.error}`, "Error");
|
let errStr = data.error;
|
||||||
|
if(errStr == undefined)
|
||||||
|
errStr = data.statusText;
|
||||||
|
alertLightbox(`Report failed: ${errStr}`, "Error");
|
||||||
}).done(data => {
|
}).done(data => {
|
||||||
alertLightbox("Report sent", "Success");
|
alertLightbox("Report sent", "Success");
|
||||||
}, "json");
|
}, "json");
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
import { getCookie, setCookie } from "./cookies";
|
import { getCookie, setCookie } from "./cookies";
|
||||||
|
|
||||||
|
|
||||||
/**
|
export function getStorageVal(key, defaultVal = "") {
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
export function getStorageVal(key, defaultVal) {
|
|
||||||
if(localStorage == undefined)
|
if(localStorage == undefined)
|
||||||
return getCookie(key, {default: defaultVal, type: "string"});
|
return getCookie(key, defaultVal);
|
||||||
let val = localStorage.getItem(key);
|
let val = localStorage.getItem(key);
|
||||||
if(val === null && defaultVal !== undefined)
|
if(val === null)
|
||||||
return defaultVal;
|
return defaultVal;
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue