Created
September 27, 2015 02:33
-
-
Save danieltdt/6672491ad2f8ece589d8 to your computer and use it in GitHub Desktop.
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 (global) { 'use strict'; | |
window.load = function () { setPreferred(getCookie()); }; | |
window.onunload = function () { setCookie(getPreferred()); }; | |
function setPreferred(style) { | |
eachAlternative(function (link, title) { | |
link.disabled = (title !== style); | |
}); | |
} | |
function getPreferred() { | |
var preferred; | |
eachAlternative(function (link, title) { | |
if (!link.disabled) { preferred = title; return false; } | |
}); | |
return preferred; | |
} | |
function getCookie() { | |
var match = /altStyle=(.*?);/.exec(document.cookie); | |
return (match ? match[1] : ''); | |
} | |
function setCookie(value) { | |
if (value) { | |
document.cookie = ':name=:value; expires=:expires; path=/' | |
.replace(':name', 'altStyle') | |
.replace(':value', value) | |
.replace(':expires', date.getTime() + daysToMili(365)); | |
} | |
} | |
function eachAlternative(iterator) { | |
var links, link, title, result; | |
links = document.getElementsByTagName('link'); | |
for (var i = 0, l = links.length; i < l; i++) { | |
link = links[i]; | |
if (~link.getAttribute('rel').indexOf('style')) { | |
title = link.getAttribute('title'); | |
if (title) { result = iterator(link, title); } | |
if (result === false) { return } | |
} | |
} | |
} | |
function daysToMili(days) { return days * 24 * 60 * 60 * 1000; } | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment