bidi-speculation-helper.js (1417B)
1 'use strict'; 2 /** 3 * Helper functions for speculation rules BiDi testdriver tests 4 */ 5 /** 6 * Waits until the document has finished loading. 7 * @returns {Promise<void>} Resolves if the document is already completely 8 * loaded or when the 'onload' event is fired. 9 */ 10 function waitForDocumentReady() { 11 return new Promise(resolve => { 12 if (document.readyState === 'complete') { 13 resolve(); 14 } 15 window.addEventListener('load', () => { 16 resolve(); 17 }, {once: true}); 18 }); 19 } 20 /** 21 * Adds speculation rules and a corresponding link to the page. 22 * @param {Object} speculationRules - The speculation rules object to add 23 * @param {string} targetUrl - The URL to add as a link 24 * @param {string} linkText - The text content of the link (optional) 25 * @returns {Object} Object containing the created script and link elements 26 */ 27 function addSpeculationRulesAndLink(speculationRules, targetUrl, linkText = 'Test Link') { 28 // Add speculation rules script exactly like the working test 29 const script = document.createElement('script'); 30 script.type = 'speculationrules'; 31 script.textContent = JSON.stringify(speculationRules); 32 document.head.appendChild(script); 33 // Also add a link to the page (some implementations might need this) 34 const link = document.createElement('a'); 35 link.href = targetUrl; 36 link.textContent = linkText; 37 document.body.appendChild(link); 38 return { script, link }; 39 }