Skip to content

Commit

Permalink
Merge pull request #9 from pols12/patch-1
Browse files Browse the repository at this point in the history
MoreMenu.js: hides core Menu if empty
  • Loading branch information
MusikAnimal committed Jul 25, 2023
2 parents 2d08aa4 + 63fd9ad commit eca41cf
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/MoreMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,58 @@ $(() => {
});
}

/**
* 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,
{
value: value,

Check failure on line 924 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Expected property shorthand
expiryDate: (new Date()).getTime() + liveTime,
}

Check failure on line 926 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Missing trailing comma
)

Check failure on line 927 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Missing semicolon
}

Check failure on line 929 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Trailing spaces not allowed
/**
* 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');

if (!obj) { //mmNativeMenuUsage nonexistent, or LocalStorage not available

Check failure on line 940 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Expected exception block, space or tab after '//' in comment
return obj;
}

//Let’s decrement value older than 24h

Check failure on line 944 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Expected exception block, space or tab after '//' in comment
if ((new Date()).getTime() > obj.expiryDate) {
obj.value--;

Check failure on line 946 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Unary operator '--' used
setStorageWithExpiry('mmNativeMenuUsage', obj.value, 86400000);
}
if (0 == obj.value) {

Check failure on line 949 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Expected '===' and instead saw '=='
mw.storage.remove('mmNativeMenuUsage');
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.
*/
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', 'vector-2022', 'timeless'].indexOf(config.currentUser.skin)
Expand All @@ -930,7 +975,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

Check failure on line 978 in src/MoreMenu.js

View workflow job for this annotation

GitHub Actions / build (16.20.0)

Expected exception block, space or tab after '//' in comment
}
}, 5000);
}
Expand Down

0 comments on commit eca41cf

Please sign in to comment.