xsanda / Run code as client

// ==UserScript==
// @exclude *
//
// ==UserLibrary==
// @name Run code as client
// @description Add a JavaScript script tag with the source of the provided
//   function as its body. Any errors are written to the console.
// @copyright 2020, xsanda (https://openuserjs.org/users/xsanda)
// @license MIT
// @version 0.1.2
// ==/UserLibrary==
//
// ==OpenUserJs==
// @author xsanda
// ==/OpenUserJs==
// ==/UserScript==

function runAsClient(f) {
  var s = document.createElement("script");
  s.type = "text/javascript";
  s.text = "(async () => { try { await (" + f.toString() + ")(); } catch (e) { console.error(e); }})();";
  document.head.appendChild(s);
}

window.runAsClient = runAsClient;