Skip to content

Instantly share code, notes, and snippets.

@gnh1201
Last active September 6, 2018 02:06
Show Gist options
  • Save gnh1201/1b4182778df5358bf879582095530a41 to your computer and use it in GitHub Desktop.
Save gnh1201/1b4182778df5358bf879582095530a41 to your computer and use it in GitHub Desktop.
ALWAYS HTTPS
/**
* @file alwayshttps.js
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Go Namhyeon <[email protected]>
* @date: 2018-02-17
*/
function alwayshttps_replace_from(uri) {
return uri.replace("http://", "https://", uri);
}
function alwayshttps_decision(a, b) {
return ('https:' == window.location.protocol) ? a : b;
}
function alwayshttps_redirect() {
if(!alwayshttps_decision(true, false)) {
location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
}
}
function alwayshttps_dom() {
var secureTagNames = [
'meta', 'link', 'script', 'embed', 'iframe',
'img', 'input', 'source', 'audio', 'video',
'track', 'a', 'form'
];
for(var i in secureTagNames) {
var doms = document.getElementsByTagName(secureTagNames[i]);
for(var k in doms) {
var domUri = "";
var domAttrName = "";
var secureDomUri = "";
switch(secureTagNames[i]) {
// meta->content
case "meta":
domAttrName = "content";
break;
// link->href, a->href
case "link":
case "a":
domAttrName = "href";
break;
// form->action
case "form":
domAttrName = "action";
break;
// script->src, embed->src, iframe->src, img->src, input->src
// source->src, audio->src, video->src, track->src
case "script":
case "embed":
case "iframe":
case "img":
case "input":
case "source":
case "audio":
case "video":
case "track":
domAttrName = "src";
break;
default:
domAttrName = "";
}
// get uri of dom element
if(doms[k] != null && typeof(doms[k].getAttribute) !== "undefined") {
domUri = doms[k].getAttribute(domAttrName);
}
// detect http, and replace to https
if(domUri != null && domUri.indexOf("http://") > -1) {
secureDomUri = alwayshttps_replace_from(domUri);
doms[k].setAttribute(domAttrName, secureDomUri);
}
}
}
}
function alwayshttps_load() {
alwayshttps_redirect();
alwayshttps_dom();
}
// load alwayshttps when ready document
if(typeof(jQuery) !== "undefined") {
jQuery(document).ready(function() { alwayshttps_load(); });
} else {
window.onload = function() { alwayshttps_load(); };
}

Add code in your website

<script type="text/javascript" src="https://cdn.rawgit.com/gnh1201/1b4182778df5358bf879582095530a41/raw/ff2130e86282bf5fd28768fcab30571633690ff6/alwayshttps.js"></script>

or install by bower

bower install alwayshttps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment