mediawiki-skins-Citizen/resources/skins.citizen.scripts/speculationRules.js
alistair3149 368ceb659a
feat(speculationRules): add basic implementation of Speculation Rules API
This is a basic implementation of Speculation Rules API that will prerender
any page under article path when the user hovers on the link for over 200ms.
2024-11-13 18:46:02 -05:00

38 lines
724 B
JavaScript

/* eslint-disable camelcase */
/**
* Initializes the Speculation Rules API for Citizen
*
* @return {void}
*/
function init() {
if (
!HTMLScriptElement.supports ||
!HTMLScriptElement.supports( 'speculationrules' )
) {
return;
}
const articlePath = mw.config.get( 'wgArticlePath' );
const article = articlePath.replace( /\$(1)/, '*' );
if ( !article ) {
return;
}
const specRules = {
prerender: [ {
where: {
href_matches: article
},
eagerness: 'moderate'
} ]
};
const specScript = document.createElement( 'script' );
specScript.type = 'speculationrules';
specScript.textContent = JSON.stringify( specRules );
document.body.append( specScript );
}
module.exports = {
init: init
};