Talk:Scripts/WME JavaScript development Discussion View history

UserScript Bootstraping

First of, shouldn't this be "bootstrapping"? The part about unsafeWindow of the bootstrap code will no longer work for Google Chrome as they have removed the bug that enabled that to work. See https://code.google.com/p/chromium/issues/detail?id=222652 Also, I'm not sure how many of our scripts actually need access to unsafeWindow. The public variables exposed by Waze generally provide enough access for anything you need to do in a script. And for all other things I've become a big fan of the MutationObserver API. Glodenox (talk) 10:02, 24 November 2015 (UTC)

Code encapsulation

I propose adding an example code block here to show what this entails. The prefixing of functions and variables in the section before will only be of importance to exposed functions at that point. My proposal for the code block, though it might be a bit much with the setTimeouts. But this should be the general template if the script wants to rely on the Waze object or attach itself to certain elements.

function init() {
  // Check all requisites for the script
  if (typeof Waze === undefined ||
      typeof document.querySelector('#WazeMap') === undefined) {
    setTimeout(init, 400);
    return;
  }
  // Initialisation or other statements using dependencies here
} 
init();

Glodenox (talk) 10:02, 24 November 2015 (UTC)

It would appear that the whole code encapsulation and function name guidelines section is outdated as userscripts are ran in a sandbox and their functions and variables are no longer exposed to the original page (and thus not to other scripts either). A new version of the bootstrap would still be interesting though. Glodenox (talk) 13:24, 29 December 2015 (UTC)