Last active
May 20, 2016 02:52
-
-
Save fakhrulhilal/4500084 to your computer and use it in GitHub Desktop.
Include all most of popular javascript libraries to the web browser.
This file contains hidden or 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 ($w, undefined) { | |
function Namespace(namespace, parent, separator) { | |
var parts = namespace.split(separator || '.'), | |
_parent = parent || window, | |
currentPart = ''; | |
for (i = 0, ii = parts.length; i < ii; i++) { | |
currentPart = parts[i]; | |
_parent[currentPart] = _parent[currentPart] || {}; | |
_parent = _parent[currentPart]; | |
} | |
return _parent; | |
} | |
$w.namespace = Namespace; | |
$w.iroel = Namespace('iroel'); | |
function loadScript(url) { | |
var script = document.createElement('script'); | |
script.setAttribute('type', 'text/javascript'); | |
script.setAttribute('src', url); | |
$w.document.body.appendChild(script); | |
return script; | |
} | |
var jsLibs = [ | |
{ name: 'jQuery', friendlyName: 'jQuery', url: '//code.jquery.com/jquery.min.js' }, | |
{ name: '_', friendlyName: 'underscore', url: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js' }, | |
{ name: 'angular', friendlyName: 'Angular', url: '//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.15/angular.min.js' }, | |
{ name: 'ko', friendlyName: 'KnockOut', url: '//cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js' }, | |
{ name: 'Backbone', friendlyName: 'Backbone', url: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js' } | |
]; | |
for (i = 0, ii = jsLibs.length; i < ii; i++) { | |
if (typeof $w[jsLibs[i].name] === 'undefined') { | |
loadScript(jsLibs[i].url); | |
$w.console.log(jsLibs[i].friendlyName + ' has been included in your browser now.'); | |
} | |
} | |
$w.iroel.loadScript = loadScript; | |
var filterLink = function (targetLink, dom) { | |
var scope = (typeof dom == 'object' && dom) || $w.document.body, output = [], link; | |
jQuery(scope).find('a[href*="' + targetLink + '"]').each(function(i, item) { | |
link = jQuery(item).attr('href'); | |
$w.console.log(link); | |
output.push(link); | |
}); | |
if (typeof dom == 'boolean' && dom) | |
return output; | |
} | |
$w.iroel.filterLink = filterLink; | |
var GetObjectKeyByValue = function (object, value) { | |
for (var prop in object) { | |
if (object.hasOwnProperty(prop)) { | |
if (object[prop] === value) | |
return prop; | |
} | |
} | |
} | |
iroel.getKeyByValue = GetObjectKeyByValue; | |
String.prototype.isEmpty = function () { | |
return !this || /^\s*^/.test(this); | |
} | |
var GetExtension = function (file) { | |
return file.substring(file.lastIndexOf('.') + 1); | |
} | |
var mimeTypes = { | |
ico: 'image/x-ico', | |
png: 'image/png', | |
gif: 'image/gif' | |
}; | |
var injectShortcut = function (icon) { | |
var iconType = "ico"; | |
//detecting icon type from parameter | |
//base64 encoded image | |
if (icon.substring(0, 4) == 'data') { | |
iconType = GetObjectKeyByValue(mimeTypes, icon.split(':')[1].split(';')[0]); | |
} | |
//from url | |
else { | |
iconType = GetObjectKeyByValue(mimeTypes, icon.substring(icon.lastIndexOf('.') + 1)); | |
} | |
var $allIcons = jQuery('head link[rel*="icon"]'), found = 0, testIcon = new RegExp(iconType); | |
if ($allIcons.length > 0) { | |
$allIcons.each(function (i, _icon) { | |
$icon = jQuery(_icon); | |
if (testIcon.test($icon.attr('type')) || | |
GetExtension($icon.attr('href')) == iconType) { | |
$icon.attr('href', icon); | |
found++; | |
} | |
}); | |
console.log('Updating ' + found + ' icons'); | |
} | |
/*if (found < 1 && $allIcons.length > 0) { | |
$allIcons.each(function (i, _icon) { | |
jQuery(_icon).attr('href', icon); | |
}); | |
console.log('Update all icon'); | |
}*/ | |
if (found < 1 || $allIcons.length == 0) { | |
jQuery('head').append('<link rel="shortcut icon" href="' + icon + '" />'); | |
console.log('Inserting new icon'); | |
} | |
} | |
$w.iroel.webIcon = injectShortcut; | |
})(window) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment