Skip to content

Instantly share code, notes, and snippets.

@kshaa
Last active July 26, 2022 08:18
Show Gist options
  • Select an option

  • Save kshaa/710e2b48e14f145703261edf41bb54a6 to your computer and use it in GitHub Desktop.

Select an option

Save kshaa/710e2b48e14f145703261edf41bb54a6 to your computer and use it in GitHub Desktop.
An example of a bootstrap JS script to inject Scala.js on any page load using browser extensions
// You can inject any Scala.js component into any page using Chrome as follows:
// - This example references an arbitrary Scala.js package called bo-search
// - bo-search contains a top level definition BoSearch.initCit
// - Install CORS Unblock extension
// - Install Custom Style Script or Scripty extension
// - Disable CORS for the page
// - Setup a local web server to host this injection script
// - Use this script to inject the component into the page
var baseURL = "http://127.0.0.1:8080"
var injectUrl = baseURL + "/inject.js" // You can even just inject this URL
var targetUrl = baseURL + "/target/scala-2.13/scalajs-bundler/main"
var library = targetUrl + "/bo-search-fastopt-library.js"
var loader = targetUrl + "/bo-search-fastopt-loader.js"
var search = targetUrl + "/bo-search-fastopt.js"
function addScript(url, callback) {
var script = document.createElement('script');
script.onload = callback
script.src = url;
document.head.appendChild(script);
}
console.log("Injecting code")
addScript(library, function () {
addScript(loader, function () {
addScript(search, function () {
console.log("Running code")
// Hack to escape Scripty extensions JavaScript execution context
var script = document.createElement("script");
script.innerHTML = "window.BoSearch.initCit()";
document.head.appendChild(script);
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment