From 8f4493c5b704b878c5aedc27a1dcdb906d9b3c3d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 26 Aug 2022 11:55:16 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20=F0=9F=90=9B=20Fix=20too-long=20U?= =?utf8?q?RLs=20on=20pages=20with=20mane=20unloaded=20elements?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit All the URLs were collected into a single URL for a GET requests and browsers and servers may not like it that long. --- src/main/resources/static/javascript/sone.js | 41 ++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 333d9d1..d9f3bf1 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -1058,12 +1058,47 @@ function checkForRemovedReplies(oldNotification, newNotification) { }); } +/** + * The URLs of not-loaded elements are part of the GET request’s URL. As + * both browsers and HTTP servers do have differing limits on URL length + * (the HTTP 1.1 RFC states 8000 bytes but most browsers only support up + * to 2000 bytes) we will return a random selection of not-loaded URLs we + * want to refresh the status from up until we are at approximately 1000 + * bytes (as the rest of the URL also needs some space). + * + * @return An array of not-loaded element URLs that will have a total length + * of 1000 bytes or fewer + */ +function getRandomSelectionOfElementsToUpdate() { + const notLoadedElementUrls = $(".linked-element.not-loaded").map(function(_, element) { + return $(element).prop("title"); + }).toArray(); + shuffleArray(notLoadedElementUrls); + const selectedElementUrls = Array(); + let currentCombinedStringLength = 0; + $(notLoadedElementUrls).each(function(_, elementUrl) { + if ((currentCombinedStringLength + elementUrl.length) <= 1000) { + selectedElementUrls.push(elementUrl); + currentCombinedStringLength += elementUrl.length; + } + }); + return selectedElementUrls; +} + +// shamelessly stolen from https://stackoverflow.com/a/12646864/43582 +function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + const temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} + function getStatus() { const parameters = isViewSonePage() ? {"soneIds": getShownSoneId()} : isKnownSonesPage() ? {"soneIds": getShownSoneIds()} : {}; $.extend(parameters, { - "elements": JSON.stringify($(".linked-element.not-loaded").map(function () { - return $(this).prop("title"); - }).toArray()) + "elements": JSON.stringify(getRandomSelectionOfElementsToUpdate()) }); ajaxGet("getStatus.ajax", parameters, function(data) { if ((data != null) && data.success) { -- 2.7.4