General 03: Javascript, Userscripts, CSS

Including jQuery

Don’t Use jquery-latest.js
jquery-latest.js isn’t meant to be included but was meant to be used an easy way to download the latest jquery version. This is stuck on version 1.11.1.

$(window).on(“load”, function(){}); vs $(document).ready(function(){});

The onload event supposedly runs much later, when content such as images have loaded. the document.ready event runs before images are loaded.

Background: As my userscripts run when new elements are detected, they tend to run more times than needed. I’ve been looking to start the dynamic checking after all page element are loaded.
(Stackoverflow: window.onload vs $(document).ready())

unsafeWindow

unsafeWindow allows me to define javascript variables that have page scope instead of function or block scope.

Background: In having common rules in my userscripts, I wanted a way to define them once so they’re accessible in different scripts. There are a few other ways to do this but I found this to be pretty simple. It is advised not to use this but it makes adding and changing rules so much easier.
(Greasemonkey: unsafeWindow)

Referencing the following Stackoverflow answer, I would believe that it’s possibly safe enough to use.
(Stackoverflow: Accessing Variables from Greasemonkey to Page & vice versa)

Userscript Scope

I’ve initially named variables with a short tag like “wr” and “lf” as I wasn’t clear on the scope of those variables in regards to other javascript variables on any given page. I’ve redefined most variables using “let” and “const” depending on their scope should be and if they could be redefined. In userscripts, I’ve learned that they aren’t normally able to interact with other scripts including other userscripts. There are some methods to share data/variables however.

General CSS

Rules that I want to remember so I don’t have to look back at css references again.

  • element1>element2

Selects “element2″s that are direct children of “element1″s.

  • element1 element2

Selects all “element2″s that are descendants of “element1″s.