luciopaiva / witchcraft

Inject Javascript and CSS right from your file system. Think GreaseMonkey for more advanced users.
https://luciopaiva.com/witchcraft
MIT License
260 stars 19 forks source link

Respond quickly to sites that don't match any script #6

Closed luciopaiva closed 6 years ago

luciopaiva commented 7 years ago

Considering that for most sites the user won't have any matching script, it would be interesting to be able to quickly return from the extension instead of hitting the file system looking for scripts that don't exist. For instance, say the user loads gist.github.com on their browser. Witchcraft would then be triggered and the follow would happen:

  1. foreground script sends request to background script
  2. background script propagates the request to the local server
  3. local server parses the hostname and collects list of files to look for
  4. ask the file system for ~/.witchcraft/com.css
  5. ask the file system for ~/.witchcraft/github.com.css
  6. ask the file system for ~/.witchcraft/gist.github.com.css
  7. ask the file system for ~/.witchcraft/com.js
  8. ask the file system for ~/.witchcraft/github.com.js
  9. ask the file system for ~/.witchcraft/gist.github.com.js
  10. return response to the background script
  11. call back foreground script with the response

We'd then hit the file system 6 times for a single page load. This happens to each and every page loaded by the browser and it gets worse if you consider that several pages also load iframes as well.

So that's why it would be good to have an in-memory set of all script names that exist under ~/.witchcraft, even if we don't keep the scripts themselves in memory, since it would immensely help pages that don't have any script to be loaded. We could just look for candidate scripts in the set and, if none is found, just return immediately.

luciopaiva commented 6 years ago

The new version has a totally different architecture, so I decided to archive this, at least for now.