Skip to content

Instantly share code, notes, and snippets.

@dwaq
Last active September 29, 2023 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwaq/8e7d04933bcee4d0aac47a3bab06a5ca to your computer and use it in GitHub Desktop.
Save dwaq/8e7d04933bcee4d0aac47a3bab06a5ca to your computer and use it in GitHub Desktop.
I use this script on my old Blogger (https://tinkeringetc.blogspot.com) to redirect to my Medium (https://medium.com/my-life-as-a-tinkerer/)
<!--
Code Flow:
* Checks page-title aginst all pages/posts
* Calls checkCookie()
* uses getCookie() to get cookie
* True: redirectPage(link);
* False: askToRedirect(link, post, false);
* Doesn't exist: askToRedirect(link, post, true);
* askToRedirect()
* puts text on screen/ in popup
* calls storeCookie() with result
* storeCookie()
* stores cookie
* calls redirectPage() if True
-->
<!--
stylize the button - similar to bootstrap's warning button
make it full width
and put a cursor over it so it appears as a link
-->
<style>
.redirectButton {
display: inline-block;
width: 100%;
color: #fff;
background-color: #f0ad4e;
border-color: #000;
font-weight: 400;
line-height: 1.25;
text-align: center;
white-space: nowrap;
vertical-align: middle;
padding: .5rem 1rem;
font-size: 1rem;
border-radius: .25rem;
cursor: pointer;
}
</style>
<!-- make an area that I can modify -->
<p id="redirect"></p>
<script>
// the class name for the title so I can see what page I'm on
var post_title = document.getElementsByClassName('post-title entry-title');
// redirect current page to passed link
function redirectPage(link) {
window.location.assign(link);
};
// store cookie related to redirection
// if true, also redirect page
function storeCookie(link, redirect) {
// cookie expiration is set as far into the future as unix allows
// http://stackoverflow.com/a/22479460/7564623
if (redirect == true) {
// store cookie as true
document.cookie = "tinkererredirect=True;expires=Tue, 19 Jan 2038 03:14:07 GMT;path=/";
// redirect page
redirectPage(link);
}
else {
// store cookie as false
document.cookie = "tinkererredirect=False;expires=Tue, 19 Jan 2038 03:14:07 GMT;path=/";
// stay on page
}
}
// ask the user if they want to move to Medium
function askToRedirect(link, post, popup) {
// change text depending if its a post or not
if (post == true) {
var text = "I have migrated my blog to Medium.";
}
else {
var text = "I have migrated this page to my personal website.";
}
// always add this text
text = text.concat(" Would you like to redirect there for the best reading experience?");
// display a confirmation box popup
if (popup == true) {
// will pass response into storeCookie()
var ask = confirm(text);
}
// add redirect button after text
text = text.concat("<br><br><button class='redirectButton'>Redirect page</button>");
// put the text and button in two places:
// 1) side bar
document.getElementById("redirect").innerHTML = text;
// 2) top bar
document.getElementsByClassName('description')[0].innerHTML = text;
// apply click handler to those buttons
// which will set a cookie and redirect the user to the correct link
var buttonsClass = document.getElementsByClassName("redirectButton");
for (var i = 0; i < buttonsClass.length; i++) {
buttonsClass[i].addEventListener('click', function(){
storeCookie(link, true);
});
}
// store cookie depending on the result from the confirmation popup
storeCookie(link, ask);
}
// get cookie from browser
// https://www.w3schools.com/js/js_cookies.asp
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// check if custom cookie exists
// and read it's value
// then redirect accordingly
function checkCookie(link, post) {
var redirect = getCookie("tinkererredirect");
// redirect page if cookie is True
if (redirect == "True") {
redirectPage(link);
}
else if (redirect == "False") {
// otherwise display information on page asking to redirect
askToRedirect(link, post, false);
}
// cookie doesn't exist
else {
// display popup for first timers
askToRedirect(link, post, true);
}
}
// main code - checks which page based on post_title
// if more than 1, you're on the main page
if (post_title.length != 1) {
checkCookie("https://medium.com/my-life-as-a-tinkerer/", true);
}
// otherwise, you're on some post
else {
switch (post_title[0].innerHTML) {
// this will trigger on the post or the page, but just redirect to page
// i.e. two things are named this
case "\nMSP430 Reaction Game\n":
checkCookie("http://tinkerer.us/projects/msp430-reaction-game.html", false);
break;
// this is for the page
case "\nHomebrew Gameboy Cartridge\n":
checkCookie("http://tinkerer.us/projects/homebrew-gameboy-cartridge.html", false);
break;
// these are the remaining posts
case "\nHow to Automatically Unzip Files Downloaded from Chrome on Windows\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/how-to-automatically-unzip-files-downloaded-from_chrome-on-windows-354032a4b048", true);
break;
case "\nParticle-Losant Integration using Webhooks\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/particle-losant-integration-using-webhooks-eb2aaafb1782", true);
break;
case "\nIoT Liquor Lights using Spark Core\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/iot-liquor-lights-using-spark-core-68b46177e808", true);
break;
case "\nSpark Lamp Update\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/spark-lamp-update-95e3aa434a54", true);
break;
case "\nSpark Lamp\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/spark-lamp-b35217f389cd", true);
break;
case "\nelectric imp-Controlled 120VAC Relay Switcher Software Update\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/electric-imp-controlled-120vac-relay-switcher-software-update-8d8d8b5c8bdc", true);
break;
case "\n1st 1/4 of '14\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/1st-1-4-of-14-84b59461e587", true);
break;
case "\nelectric imp-Controlled 120VAC Relay Switcher\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/electric-imp-controlled-120vac-relay-switcher-6219200ad7ea", true);
break;
case "\nPast Few Months\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/past-few-months-de4b20ae22a7", true);
break;
case "\nAdjustable RGB LED Color Cube\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/adjustable-rgb-led-color-cube-7573d160d38f", true);
break;
case "\nProgramming Arduino Bootloader on ATmega328p with ZeptoProg II\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/programming-arduino-bootloader-on-atmega328p-with-zeptoprog-ii-869515c57a6", true);
break;
case "\nCloset Door Lightswitch\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/closet-door-lightswitch-fb3a25d75f9d", true);
break;
case "\nWhy I've Been Gone\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/why-ive-been-gone-ca4044598149", true);
break;
case "\nGoogle Chrome Remote Webcam Viewer\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/google-chrome-remote-webcam-viewer-4d8f12e9518e", true);
break;
case "\nFilament Spool Stand\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/filament-spool-stand-4e296ed34670", true);
break;
case "\nMy 3D Prints Summary #1\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/my-3d-prints-summary-1-ab2b12135f7f", true);
break;
case "\nPrintrbot Calibration Prints\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/printrbot-calibration-prints-39c8b0f1b2e4", true);
break;
case "\nAutomatic Garden Waterer: Part 2\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/automatic-garden-waterer-part-2-b54c2f594541", true);
break;
case "\nPrintrbot Tip: Leveling the Printrbed\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/printrbot-tip-leveling-the-printrbed-bf945e115d74", true);
break;
case "\nPrintrbot Build: Part 2\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/printrbot-build-part-2-30951248ff7d", true);
break;
case "\nPrintrbot Build: Part 1\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/printrbot-build-part-1-3fc1060bdb5", true);
break;
case "\nAutomatic Garden Waterer: Part 1\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/automatic-garden-waterer-part-1-9fc4b080df34", true);
break;
case "\nFather's Day Phones\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/fathers-day-phones-4af04a87533b", true);
break;
case "\nSome More Shelves\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/some-more-shelves-6b9c3c29b1c8", true);
break;
case "\nContinuity Checker\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/continuity-checker-4d7f15dc356c", true);
break;
case "\nHow (Not) To Fix A Stereo Amplifier\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/how-not-to-fix-a-stereo-amplifier-5c1fc7d86ed6", true);
break;
case "\n5V Regulated Breadboard Power Supply\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/5v-regulated-breadboard-power-supply-7edc2d97d110", true);
break;
case "\nHomemade Variable Power Supply\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/homemade-variable-power-supply-186ec1b75aa2", true);
break;
case "\nMaking LadyAda's MiniPOV3\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/making-ladyadas-minipov3-4dd92b694166", true);
break;
case "\nI'm Screwed\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/im-screwed-bb1bb72d2b44", true);
break;
case "\nEasy Documentation Organization\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/easy-documentation-organization-ba706a35f0b5", true);
break;
case "\nBeautiful iPhone Protection (for Cheap!)\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/beautiful-iphone-protection-for-cheap-1ac4316aa5a", true);
break;
case "\nI'm Back!\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/im-back-d2161426fbf8", true);
break;
case "\nBus Pirate v4 Assembly\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/bus-pirate-v4-assembly-d55eb5b1d716", true);
break;
case "\nStorage Container Shelving System\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/storage-container-shelving-system-43c601c46bd9", true);
break;
case "\nCrater Lake National Park\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/crater-lake-national-park-1204b79e6cdd", true);
break;
case "\nGoogle Calendar/Contact Integration with iPhone\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/google-calendar-contact-integration-with-iphone-a47dcfc5ce34", true);
break;
case "\nNewegg Computer Combo Assembly\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/newegg-computer-combo-assembly-aa276d3fcfc4", true);
break;
case "\nTI FRAM MSP-EXP430 Overview\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/ti-fram-msp-exp430-overview-cb0623bae6da", true);
break;
case "\nHomemade PID Controlled Hotplate\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/homemade-pid-controlled-hotplate-57a031358ca0", true);
break;
case "\nLinksys LED Matrix\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/linksys-led-matrix-470663a3d7ea", true);
break;
case "\nMultimeter Fuse Hack\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/multimeter-fuse-hack-21fb2449b602", true);
break;
case "\nLinksys LED Matrix (Intro)\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/linksys-led-matrix-intro-cd11b9fc2ac3", true);
break;
case "\nFlower Garden &amp; Border\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/flower-garden-border-d760ff94e0a", true);
break;
case "\nGarden Soaker Hose Tip\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/garden-soaker-hose-tip-b66c397364d6", true);
break;
case "\nMicro SD Card Keychain Dissasembly\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/micro-sd-card-keychain-dissasembly-36a069633d31", true);
break;
case "\nDVD Rack\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/dvd-rack-ea0f67b6ba0d", true);
break;
case "\nGardening\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/gardening-1ec000ebc3a8", true);
break;
case "\nIntroduction\n":
checkCookie("https://medium.com/my-life-as-a-tinkerer/introduction-b62448839ebb", true);
break;
default:
// not found - just redirect to homepage
checkCookie("https://medium.com/my-life-as-a-tinkerer/", true);
break;
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment