messaging-blob-helpers.js (2028B)
1 'use strict'; 2 3 // Creates a blob URL with the contents of 'message-target.html'. Use the 4 // blob as an iframe src or a window.open() URL, which creates a same origin 5 // message target. 6 async function create_message_target_blob_url(test) { 7 const html = await create_message_target_html_without_subresources(test); 8 const blob = new Blob([html], { type: 'text/html' }); 9 return URL.createObjectURL(blob); 10 } 11 12 // Creates a data URI with the contents of 'message-target.html'. Use the 13 // data URI as an iframe src, which creates a cross origin message target. 14 async function create_message_target_data_uri(test) { 15 const iframe_html = 16 await create_message_target_html_without_subresources(test); 17 return `data:text/html,${encodeURIComponent(iframe_html)}`; 18 } 19 20 // Constructs a version of 'message-target.html' without any subresources. 21 // Enables the creation of blob URLs, data URIs and iframe srcdocs re-using 22 // the contents of 'message-target.html'. 23 async function create_message_target_html_without_subresources(test) { 24 const test_helpers_script = 25 await fetch_text('../fs/resources/test-helpers.js'); 26 27 const messaging_helpers_script = 28 await fetch_text('../fs/resources/messaging-helpers.js'); 29 30 const messaging_serialize_helpers_script = 31 await fetch_text('../fs/resources/messaging-serialize-helpers.js'); 32 33 const message_target_script = 34 await fetch_text('../fs/resources/message-target.js'); 35 36 // Get the inline script code from 'message-target.html'. 37 const iframe = 38 await add_iframe(test, {src: '../fs/resources/message-target.html'}); 39 const iframe_script = 40 iframe.contentWindow.document.getElementById('inline_script').outerHTML; 41 iframe.remove(); 42 43 return '<!DOCTYPE html>' + 44 `<script>${test_helpers_script}</script>` + 45 `<script>${messaging_serialize_helpers_script}</script>` + 46 `<script>${message_target_script}</script>` + 47 `${iframe_script}`; 48 } 49 50 async function fetch_text(url) { 51 const response = await fetch(url); 52 return await response.text(); 53 }