Difference between revisions of "Talk:Scripts/WME JavaScript development"

From Wazeopedia
(Created page with "== 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...")
 
Line 6: Line 6:
  
 
== Code encapsulation ==
 
== 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.
+
<s>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.</s>
 
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.
 
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.
<pre>(function() {
+
<pre>function init() {
   function init() {
+
   // Check all requisites for the script
    // Check all requisites for the script
+
  if (typeof Waze === undefined ||
    if (typeof Waze === undefined ||
+
      typeof document.querySelector('#WazeMap') === undefined) {
        typeof document.querySelector('#WazeMap') === undefined) {
+
    setTimeout(init, 400);
      setTimeout(init, 400);
+
    return;
      return;
 
    }
 
    // Initialisation or other statements using dependencies here
 
 
   }
 
   }
   init();
+
   // Initialisation or other statements using dependencies here
})();</pre>
+
}  
 +
init();</pre>
 
[[User:Glodenox|Glodenox]] ([[User talk:Glodenox|talk]]) 10:02, 24 November 2015 (UTC)
 
[[User:Glodenox|Glodenox]] ([[User talk: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. [[User:Glodenox|Glodenox]] ([[User talk:Glodenox|talk]]) 13:24, 29 December 2015 (UTC)

Revision as of 13:24, 29 December 2015

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)