User:Ketil3/Programming

Some programming things. Based on

In my own common.js edit

Javascript edit

I want a gallery option that recursively (up to a tunable limit) shows media content in subcategories.

Use/place template it in wikitext (the normal text in a page)

Template:RecuGall

Define template: Template:recuGall to specify a DIV with id=Y. Nothing more.

{{

}}

Define the code edit

(in e.g. common.js or ... in my user space, preferably)

( function() {
    var el = document.getElement ('Y');
    el.innerHTML = CONTENT;
  }
)

I think placing the (anonymous) function within paranthesis causes it (the function) to be executed, not just defined. The execution takes place at once, immediately after the definitioni has been interpreted. Some call it "self-execution". even jQuery can be used (quite similar, but notice...):

$( function() {             # notice the $-sign!!
   $('Y').html = CONTENT;
} () );                     # notice the extra parantheses!!

The $-sign (first line) is shorthand for the $(document).ready event, causing the anonymous function to be run once the entire HTML document has been received, interprete3d and loaded.

They suggest adding JS to the MediaWiki:Commons.js

Other JS-files (such as Example.js) can be included from JS using the mw library:

mw.loader.load ('/w/index.php?title=Mediawiki:Example.js&action=raw&cType=text/javascript");

Security edit

When it runs, the JS program has "user rights", meaning it has the same privileges (rights) as the user (you). With respect to Wikipedia resources and your account, I think.

Existing user JS scripts edit

Locate a promising piece, preferraby written to work in Wikipedia

directs to (among others):

where the "catwatch" seems interesting because of its name:

describes it as a program that adds things to my (the user) watchlist, events such a page being added to a category. It has a direct link to the program

  • [[:en:User:Ais523/catwatch.js

It seems to store the catwatchlist in

User:.../WatchedCategories.js

and has the following structure

 addLoadHook (...)    // hooked on for execution when the wikitext has been loaded
    get the catwatchlist (using Ajax)
    if nothing found,
        ask the user if he/she wants to create the catwatchlist, and if OK,
           cause the next PUT to be a "create" on the watchlist
    else
       for each watched category
          get members in the category (Ajax), add to internal array CW

A hello messagebox perhaps edit

  addLoadHook(...)
     alert("Hello")

(annoying since every page will do this)

A page counter edit

This is how I change the above (catwatch) to create a "counter", oh-so popular in the mid 90s.

JS

  storageURL = User:.../Counter.js
     format:  Just an integer that increments by one each time
  addLoadHook (...)
     get current counter (from storageURL) ... or set it to zero (0)
     add 1 to counter
     store counter (to storageURL)