Page MenuHomePhabricator

add DOM friendly importScript and importStylesheet functions into wikibits.js
Closed, ResolvedPublic


Author: herd

tab-indented plaintext of proposed functions, per

Per discussions on mediawiki javascript bloat, and dom-safe script/style functions in common use on the major wikis, users in #mediawiki-scripts have written some browser-safe import*() functions for CSS and JS, which could be added to wikibits.js, and replace many instances of document.write()(which can be buggy in safari and other browsers) as well as give script writers more options to optionally include libraries.

This proposed version (5 functions with little redundancy, importScript(), importScriptURI(), importStylesheet(), importStylesheetURI(), and appendCSS() -- see attachment, sorry it isn't a patch) maintain full backward compatabilty with existing copies of importScript() and importStylesheet() (as used on the English Wikipedia), and fix a few bugs in IE and Safari as well. They have been tested on the following browsers:

  • Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.5a) Gecko/20030728 Mozilla Firebird/0.6.1 (Windows 98)
  • Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/ (Linux)
  • Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b3pre) Gecko/2008020507 Firefox/3.0b3pre (Linux)
  • Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20071128 Iceweasel/ (Debian- (Linux)
  • Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv: Gecko/20080201 Firefox/ (Windows XP)
  • Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.2) (Windows XP)
  • Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.0.3705; .NET CLR 1.1.4322; rm ; .NET CLR 2.0.50727) (Win XP)
  • Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.15.1 (KHTML, like Gecko) Version/3.0.4 Safari/523.15
  • Opera/9.10 (Macintosh; Intel Mac OS X; U; en)
  • Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/20080201 Firefox/
  • Opera/8.5 (Macintosh; Intel Mac OS X; U; en)

The functions should be added near the top, but probably below function addOnloadHook() and function hookEvent().

The current attachment version is from (which differs from the tested version only in function names and appearance).

Version: unspecified
Severity: enhancement




Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 10:06 PM
bzimport set Reference to bz13232.
bzimport added a subscriber: Unknown Object (MLST).

skizzerz wrote:

patch of changes against current trunk

added patch of the diff between current trunk and Splarka's modifications as per Splarka's request on IRC

attachment diff.patch ignored as obsolete

Created attachment 4696
Updated plaintext with new return statements. Taken from

This new plaintext version of the proposed functions adds a few return statements.

These can be useful in order to do things like:
var s=importScript('User:AzaToth/morebits.js');
s.onload = function loadRestOfTwinkle(){

And other changes or checks of the added DOM element.


skizzerz wrote:

re-diffed against current trunk with updates

attachment diff2.patch ignored as obsolete

herd wrote:

(In reply to comment #4)

Created an attachment (id=4697) [details]
re-diffed against current trunk with updates

Oops, 4697 has an extranuous semicolon in it, I probably sent the wrong file to Ryan.
+ onloadFuncts[onloadFuncts.length]; = hookFunct;
should be
+ onloadFuncts[onloadFuncts.length] = hookFunct;

Also, only the original patch was tested against all the listed browsers, the feature creeping may get to the point of invalidating those tests somewhat.

Also, please send all future suggested changes as unified patches (or we'll make a mess, and #mediawiki-scripts will be laughed at).

Created attachment 4701
Updated diff. removed extraneous semicolon and ^M chars


If a script requires JavaScript 1.7 or 1.8 this needs to be declared in the <script> tag.

To make this possible importScript needs an optional JavaScript version parameter.

We discussed this a bit in #mediawiki-scripts

We currently do not see the need to add this. 1.7 and 1.8 features are hardly used. If someone does need it, we can add it easily enough at a later time.

Looks pretty sweet. :D Applied in r35064