function fixJSForCodeSite(js) {
  if (/requestCORS/.test(js)) {
    return js;
  }

  let found = false;
  js = js.replace(/^( +)(img|image)(\.src = )(.*?);.*?$/mg, function(match, indent, variable, code, url) {
    found = true;
    return `${indent}requestCORSIfNotSameOrigin(${variable}, ${url})
${indent}${variable}${code}${url};`;
  });
  if (found) {
    js += `

// This is needed if the images are not on the same domain
// NOTE: The server providing the images must give CORS permissions
// in order to be able to use the image with WebGL. Most sites
// do NOT give permission.
// See: https://webgpufundamentals.org/webgl/lessons/webgl-cors-permission.html
function requestCORSIfNotSameOrigin(img, url) {
  if ((new URL(url, window.location.href)).origin !== window.location.origin) {
    img.crossOrigin = "";
  }
}
`;
  }
  return js;
}
/**
 * Change JavaScript before uploading code to JSFiddle/Codepen
 *
 * @param {string} js JavaScript source
 * @returns {string} The JavaScript source with any fixes applied.
 */
Change JavaScript before uploading code to JSFiddle/Codepen
@param {string} js JavaScript source
@returns {string} The JavaScript source with any fixes applied.
Change JavaScript before uploading code to JSFiddle / Codepen @param { string } js JavaScript source @returns { string } The JavaScript source with any fixes applied .
function fixJSForCodeSite(js) {
  if (/requestCORS/.test(js)) {
    return js;
  }
  let found = false;
  js = js.replace(/^( +)(img|image)(\.src = )(.*?);.*?$/mg, function(match, indent, variable, code, url) {
    found = true;
    return `${indent}requestCORSIfNotSameOrigin(${variable}, ${url})
${indent}${variable}${code}${url};`;
  });
  if (found) {
    js += `
// This is needed if the images are not on the same domain
// NOTE: The server providing the images must give CORS permissions
// in order to be able to use the image with WebGL. Most sites
// do NOT give permission.
// See: https://webgpufundamentals.org/webgl/lessons/webgl-cors-permission.html
function requestCORSIfNotSameOrigin(img, url) {
  if ((new URL(url, window.location.href)).origin !== window.location.origin) {
    img.crossOrigin = "";
  }
}
`;
  }
  return js;
}
function fixJSForCodeSite ( js ) { if ( / requestCORS / . test ( js ) ) { return js ; } let found = false ; js = js . replace ( / ^( +)(img|image)(\.src = )(.*?);.*?$ / mg , function ( match , indent , variable , code , url ) { found = true ; return ` ${ indent } ${ variable } ${ url } ${ indent } ${ variable } ${ code } ${ url } ` ; } ) ; if ( found ) { js += ` ` ; } return js ; }
Change JavaScript before uploading code to JSFiddle/Codepen
 @param {string} js JavaScript source
 @returns {string} The JavaScript source with any fixes applied.
Change JavaScript before uploading code to JSFiddle / Codepen @param { string } js JavaScript source @returns { string } The JavaScript source with any fixes applied .