Skip to content

Commit

Permalink
add script
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidely committed May 2, 2024
1 parent 59c2bcd commit dcb5672
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"styles": [
"styles/ext.networknotice.Notice.less"
],
"scripts": [
"scripts/ext.networknotice.Notice.js"
],
"position": "bottom"
}
},
Expand Down
56 changes: 56 additions & 0 deletions resources/scripts/ext.networknotice.Notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
( function ( window, document ) {
'use strict';

const LOCAL_STORAGE_KEY = 'networknotice';

function init() {
if ( 'localStorage' in window ) {
document.querySelectorAll( '[data-component="network-notice"]' ).forEach( function( notice ) {
const key = notice.dataset.id;
if ( isInStorage( key ) ) {
notice.classList.add( 'd-none' );
} else {
const closeButton = notice.querySelector(
'[data-component="network-notice-close-button"]'
);
closeButton.onclick = function() {
notice.classList.add( 'd-none' );
putIntoStorage( key );
};
}
} );
}
}

function getItemsFromStorage() {
const items = localStorage.getItem( LOCAL_STORAGE_KEY );
return items ? JSON.parse( items ) : [ ];
}

function putIntoStorage( key ) {
const items = getItemsFromStorage();
if ( !items.includes( key ) ) {
items.push( key );
localStorage.setItem( LOCAL_STORAGE_KEY, JSON.stringify( items ) );
}
}

function isInStorage( key ) {
const items = getItemsFromStorage();
return items.includes( key );
}

/**
* Check on document readyState
*/
if ( document.readyState === 'complete' ) {
init();
} else {
document.addEventListener( 'readystatechange', () => {
if ( document.readyState === 'complete' ) {
init();
}
} );
}

} ( window, document ) );
1 change: 1 addition & 0 deletions src/Hooks/MainHookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MainHookHandler implements
*/
public function onBeforePageDisplay( $out, $skin ): void {
$out->addModuleStyles( 'ext.networknotice.Notice' );
$out->addModules( 'ext.networknotice.Notice' );
}

/**
Expand Down

0 comments on commit dcb5672

Please sign in to comment.