Last active
July 26, 2022 08:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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