Remove ajax_auth.js lib, by merging it in where needed

It's only actually used in two JS files, so rather than doing a weird
global `$.ajaxSetup` call, let's just inline it into the small handful
of AJAX calls that actually care.
This commit is contained in:
Emi Matchu 2024-09-20 19:10:26 -07:00
parent f20a1b5398
commit 31619071af
6 changed files with 22 additions and 24 deletions

View file

@ -1,20 +0,0 @@
(function () {
var CSRFProtection;
var token = $('meta[name="csrf-token"]').attr("content");
if (token) {
CSRFProtection = function (xhr, settings) {
var sendToken =
typeof settings.useCSRFProtection === "undefined" || // default to true
settings.useCSRFProtection;
if (sendToken) {
xhr.setRequestHeader("X-CSRF-Token", token);
}
};
} else {
CSRFProtection = $.noop;
}
$.ajaxSetup({
beforeSend: CSRFProtection,
});
})();

View file

@ -1,4 +1,11 @@
(function () { (function () {
function addCSRFToken(xhr) {
const token = document
.querySelector('meta[name="csrf-token"]')
?.getAttribute("content");
xhr.setRequestHeader("X-CSRF-Token", token);
}
var hangersInitCallbacks = []; var hangersInitCallbacks = [];
function onHangersInit(callback) { function onHangersInit(callback) {
@ -285,6 +292,7 @@
type: "post", type: "post",
data: data, data: data,
dataType: "json", dataType: "json",
beforeSend: addCSRFToken,
complete: function (data) { complete: function (data) {
if (quantityEl.val() == 0) { if (quantityEl.val() == 0) {
objectRemoved(objectWrapper); objectRemoved(objectWrapper);
@ -389,6 +397,7 @@
type: "post", type: "post",
data: data, data: data,
dataType: "json", dataType: "json",
beforeSend: addCSRFToken,
complete: function () { complete: function () {
button.val("Remove"); button.val("Remove");
}, },
@ -465,6 +474,7 @@
url: form.attr("action"), url: form.attr("action"),
type: form.attr("method"), type: form.attr("method"),
data: data, data: data,
beforeSend: addCSRFToken,
success: function (html) { success: function (html) {
var doc = $(html); var doc = $(html);
hangersEl.html(doc.find("#closet-hangers").html()); hangersEl.html(doc.find("#closet-hangers").html());
@ -501,6 +511,7 @@
url: form.attr("action") + ".json?" + $.param({ ids: hangerIds }), url: form.attr("action") + ".json?" + $.param({ ids: hangerIds }),
type: "delete", type: "delete",
dataType: "json", dataType: "json",
beforeSend: addCSRFToken,
success: function () { success: function () {
objectRemoved(hangerEls); objectRemoved(hangerEls);
}, },
@ -567,6 +578,7 @@
closet_hanger: closetHanger, closet_hanger: closetHanger,
return_to: window.location.pathname + window.location.search, return_to: window.location.pathname + window.location.search,
}, },
beforeSend: addCSRFToken,
complete: function () { complete: function () {
itemsSearchField.removeClass("loading"); itemsSearchField.removeClass("loading");
}, },
@ -711,6 +723,7 @@
type: "post", type: "post",
data: data, data: data,
dataType: "json", dataType: "json",
beforeSend: addCSRFToken,
complete: function () { complete: function () {
contactForm.enableForms(); contactForm.enableForms();
}, },
@ -731,6 +744,7 @@
type: "POST", type: "POST",
data: { neopets_connection: { neopets_username: newUsername } }, data: { neopets_connection: { neopets_username: newUsername } },
dataType: "json", dataType: "json",
beforeSend: addCSRFToken,
success: function (connection) { success: function (connection) {
var newOption = $("<option/>", { var newOption = $("<option/>", {
text: newUsername, text: newUsername,

View file

@ -37,6 +37,12 @@
pets.shift(); pets.shift();
loading = true; loading = true;
$.ajax({ $.ajax({
beforeSend: (xhr) => {
const token = document
.querySelector('meta[name="csrf-token"]')
?.getAttribute("content");
xhr.setRequestHeader("X-CSRF-Token", token);
},
complete: function (data) { complete: function (data) {
loading = false; loading = false;
loadNextIfReady(); loadNextIfReady();

View file

@ -152,8 +152,7 @@
- content_for :javascripts do - content_for :javascripts do
= include_javascript_libraries :jquery, :jquery_tmpl = include_javascript_libraries :jquery, :jquery_tmpl
= javascript_include_tag 'ajax_auth', 'jquery.ui', 'jquery.jgrowl', = javascript_include_tag 'jquery.ui', 'jquery.jgrowl', defer: true
defer: true
- content_for :javascripts_body do - content_for :javascripts_body do
= javascript_include_tag 'closet_hangers/index', defer: true = javascript_include_tag 'closet_hangers/index', defer: true

View file

@ -122,7 +122,7 @@
- content_for :javascripts do - content_for :javascripts do
= include_javascript_libraries :jquery, :jquery_tmpl = include_javascript_libraries :jquery, :jquery_tmpl
= javascript_include_tag 'ajax_auth', 'jquery.timeago', defer: true = javascript_include_tag 'jquery.timeago', defer: true
- content_for :javascripts_body do - content_for :javascripts_body do
= javascript_include_tag 'outfits/new', defer: true = javascript_include_tag 'outfits/new', defer: true

View file

@ -53,7 +53,6 @@
- content_for :javascripts do - content_for :javascripts do
= include_javascript_libraries :jquery, :jquery_tmpl = include_javascript_libraries :jquery, :jquery_tmpl
= javascript_include_tag 'ajax_auth', defer: true
- content_for :javascripts_body do - content_for :javascripts_body do
= javascript_include_tag 'pets/bulk', defer: true = javascript_include_tag 'pets/bulk', defer: true