From 1d67dab9a18179bb0a8f803dc1dc852eb5369976 Mon Sep 17 00:00:00 2001 From: pols12 Date: Sat, 13 Mar 2021 15:04:48 +0100 Subject: [PATCH 1/4] MoreMenu.js: hides core Menu if empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hides div#p-cactions which used to contain “Move” link and which is now empty. Fixes #6 --- src/MoreMenu.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/MoreMenu.js b/src/MoreMenu.js index f14292b..db5ba69 100644 --- a/src/MoreMenu.js +++ b/src/MoreMenu.js @@ -960,6 +960,11 @@ $(() => { */ function removeUnneededLinks() { handleHistoryAndWatchLinks(true); + + /* Hides core More menu if it is empty */ + if( $('#p-cactions ul li').length === 0 ) { + $('#p-cactions').hide(); + } /** Following logic only applies to the User menu. */ if (!config.targetUser.name) { From d97b962070536e85f01cce1f023631923c228408 Mon Sep 17 00:00:00 2001 From: pols12 Date: Wed, 24 Mar 2021 15:11:08 +0100 Subject: [PATCH 2/4] Reverts Hiding old More Menu has already been supported through a MutationObserver. --- src/MoreMenu.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/MoreMenu.js b/src/MoreMenu.js index db5ba69..f14292b 100644 --- a/src/MoreMenu.js +++ b/src/MoreMenu.js @@ -960,11 +960,6 @@ $(() => { */ function removeUnneededLinks() { handleHistoryAndWatchLinks(true); - - /* Hides core More menu if it is empty */ - if( $('#p-cactions ul li').length === 0 ) { - $('#p-cactions').hide(); - } /** Following logic only applies to the User menu. */ if (!config.targetUser.name) { From 2057864639eccffad37ab45e688c215ece7812a2 Mon Sep 17 00:00:00 2001 From: pols12 Date: Wed, 24 Mar 2021 15:39:34 +0100 Subject: [PATCH 3/4] MoreMenu.js: implements storage with expiry Adds setStorageWithExpiry() and getStorageWithExpiry() which should be used instead of mw.storage.get() and mw.storage.set() for data with expiry time. They should be used by mmNativeMenuUsage. --- src/MoreMenu.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/MoreMenu.js b/src/MoreMenu.js index f14292b..b6623ef 100644 --- a/src/MoreMenu.js +++ b/src/MoreMenu.js @@ -875,6 +875,27 @@ $(() => { }); } + function setStorageWithExpiry(key, value, liveTime) { + return mw.storage.setObject( + key, + { + value: value, + expiryDate: (new Date()).getTime() + liveTime, + } + ) + } + + function getStorageWithExpiry(key) { + const obj = mw.storage.getObject(key) + + if ((new Date()).getTime() > obj.expiryDate) { + mw.storage.remove(key) + return null + } + + return obj.value + } + /** * For Vector/Timeless's native More menu, we keep track of how many times it gets populated over time, * to intelligently determine whether we should leave it upfront to make the script feel more responsive. From 63fd9adb71be54ba9d1439ba04c8c3b1d9c5efae Mon Sep 17 00:00:00 2001 From: pols12 Date: Fri, 9 Apr 2021 02:24:08 +0200 Subject: [PATCH 4/4] +getMmNativeMenuUsage() decrementing 24h-old value Instead of a full expiry for nativeMenuUsage counter, implements a progressive expiry: every 24h, counter decreases and is removed when it is 0. --- src/MoreMenu.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/MoreMenu.js b/src/MoreMenu.js index b6623ef..446a2f6 100644 --- a/src/MoreMenu.js +++ b/src/MoreMenu.js @@ -875,6 +875,14 @@ $(() => { }); } + /** + * Stores in LocalStorage with an expiry date (now + liveTime, in ms). + * + * @param {string} key Key name to store under + * @param {Object} value Object value to be stored + * @param {int} liveTime Delay in ms before to consider the value as outdated + * @return {boolean} The value was set + */ function setStorageWithExpiry(key, value, liveTime) { return mw.storage.setObject( key, @@ -884,16 +892,32 @@ $(() => { } ) } + + /** + * Retrieve mmNativeMenuUsage from LocalStorage and decrements it if + * its expiry date has been passed. + * + * @return {int|null|boolean} mmNativeMenuUsage, null if no value exists, + * or false if storage is not available. + */ + function getMmNativeMenuUsage() { + const obj = mw.storage.getObject('mmNativeMenuUsage'); - function getStorageWithExpiry(key) { - const obj = mw.storage.getObject(key) + if (!obj) { //mmNativeMenuUsage nonexistent, or LocalStorage not available + return obj; + } + //Let’s decrement value older than 24h if ((new Date()).getTime() > obj.expiryDate) { - mw.storage.remove(key) - return null + obj.value--; + setStorageWithExpiry('mmNativeMenuUsage', obj.value, 86400000); + } + if (0 == obj.value) { + mw.storage.remove('mmNativeMenuUsage'); + return null; } - return obj.value + return obj.value; } /** @@ -902,7 +926,7 @@ $(() => { */ function observeCactionsMenu() { /** Check local storage to see if user continually has items added to the native menu. */ - const reAddCount = parseInt(mw.storage.get('mmNativeMenuUsage'), 10) || 0; + const reAddCount = parseInt(getMmNativeMenuUsage(), 10) || 0; /** Ignore for non-Vector/Timeless, if user disabled this feature, or if reAddCount is high. */ if (-1 === ['vector', 'timeless'].indexOf(config.currentUser.skin) @@ -917,7 +941,7 @@ $(() => { /** Wait 5 seconds before checking the reAddCount, to give other scripts time to populate the menu */ setTimeout(() => { if ($('#p-cactions').find('li').length) { - mw.storage.set('mmNativeMenuUsage', reAddCount + 1); + setStorageWithExpiry('mmNativeMenuUsage', reAddCount + 1, 86400000); //24h liveTime } }, 5000); }