Last active
October 4, 2017 21:48
-
-
Save Colouratura/405fea83e7fd9fe29783bf928eba1643 to your computer and use it in GitHub Desktop.
The script loader for Wikia that lets you use ES6 like an actual person
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (window, document, mw) { | |
var ZION = function () { | |
this._scripts = []; | |
this.loaded = this.loaded(); | |
return this; | |
}; | |
ZION.prototype._getScriptURL = function (script) { | |
var split = script.split(':'); | |
if (split.length === 0) return null; | |
if (split.length >= 1) { | |
if (split[0] === 'u') { | |
split.shift(); | |
var server = split.shift(), | |
page = split.join(':'); | |
return 'http://' + server + '.wikia.com/wiki/' + page + '?action=raw&ctype=text/javascript'; | |
} else { | |
var page = (split.length > 1) ? split.join(':') : split[0]; | |
return mw.config.get('wgServer') + '/wiki/' + page + '?action=raw&ctype=text/javascript'; | |
} | |
} | |
}; | |
ZION.prototype._loadScript = function (script) { | |
var node = document.createElement('script'); | |
var src = this._getScriptURL(script); | |
if (src === null) this.loaded.fire(); | |
node.setAttribute('src', src); | |
node.setAttribute('async', 'true'); | |
document.body.appendChild(node); | |
}; | |
ZION.prototype.load = function (scripts) { | |
if (typeof scripts !== undefined) { | |
if (Array.isArray(scripts)) { | |
this._scripts = scripts; | |
} else { | |
this._scripts = [scripts]; | |
} | |
this.loaded.fire(); | |
} | |
}; | |
ZION.prototype.loaded = function () { | |
return mw.hook('ZION.loaded').add(function () { | |
if (this._scripts.length === 0) return; | |
this._loadScript(this._scripts.shift()); | |
}.bind(this)); | |
}; | |
if (window.ZION !== undefined) return; | |
window.ZION = new ZION(); | |
}(window, document, mw)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment