Advertisement
DanAtkinson

MoneySavingExpert redirect to tips in email

Jan 3rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Fix MSE Tip emails
  3. // @namespace    http://danbo.me
  4. // @version      1.1
  5. // @description  This was created on a rainy Sunday because MSE emails always point the 'view online' link to their latest tip.
  6. // @description  On countless occasions, I've actually wanted to go to an older tip.
  7. // @description  This takes the GTM/Analytics-friendly utm params and parses out the date to find a matching tip page.
  8. // @author       Dan Atkinson 2016-09-25
  9. // @match        http://www.moneysavingexpert.com/latesttip/*
  10. // @require      https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
  11. // ==/UserScript==
  12.  
  13. (function($) {
  14.     'use strict';
  15.  
  16.     var i, utmTerm, utmDate, splitParams, dateRegex, getFormattedDate;
  17.  
  18.     dateRegex = new RegExp('((([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\\d{2})');
  19.  
  20.     getFormattedDate = function (d) {
  21.         var day, month, year;
  22.  
  23.         day = d.getDate();
  24.         day = day < 10 ? '0' + day : day;
  25.  
  26.         month = d.getMonth() + 1;
  27.         month = month < 10 ? '0' + month : month;
  28.  
  29.         year = d.getFullYear().toString().substring(2);
  30.  
  31.         return day + '-' + month + '-' + year;
  32.     };
  33.  
  34.     if (new Date().getFullYear() === 2017) {
  35.         console.log('New Year\'s resolution: It\'s been too long, Dan. You... You should tell MSE about this and ask them to fix it with permalinks in new emails and some kinda redirect to handle their old emails.');
  36.     }
  37.  
  38.     //First, let's grab the params for the email. These appear to be stored against the utm_term param.
  39.     splitParams = location.search.split('&');
  40.  
  41.     //Default for location.search with no params is '' so a split will return an array with a single empty item. If there are none, we want to go to the latest tip.
  42.     if (splitParams.length === 1 && splitParams[0] === '') {
  43.         return;
  44.     }
  45.  
  46.     //Parse out the utm_term param, if it exists.
  47.  
  48.     for (i = 0; i < splitParams.length; i += 1) {
  49.       if (splitParams[i].startsWith('utm_term=')) {
  50.           utmTerm = splitParams[i].substring(9); //Remove 'utm_term='.
  51.           break;
  52.       }
  53.     }
  54.  
  55.     //Is the value blank?
  56.     if (utmTerm === '') {
  57.         //No utm_term found. Do nothing more.
  58.         return;
  59.     }
  60.  
  61.     //Execute the regex. The first 9 chars always seem to be the date (dd-MMM-yy) followed by the version - eg: '14-Jun-16-v1'. I'll allow it anywhere, so I haven't set the ^ in the regex.
  62.     utmDate = dateRegex.exec(utmTerm);
  63.     if (utmDate === null) {
  64.         return;
  65.     }
  66.  
  67.     //We should now have our date
  68.     utmDate = new Date(utmDate[0]);
  69.     if (utmDate.toString() === 'Invalid date') {
  70.         return;
  71.     }
  72.  
  73.     i = 0;
  74.  
  75.     //Now attempt to get this date. Try up to seven days. It's not always the same day...
  76.     while (i < 7) {
  77.         $.get('http://www.moneysavingexpert.com/tips/' + getFormattedDate(utmDate) + '/').done(function () {
  78.             //Changing the location will break the loop.
  79.             location.href = this.url;
  80.         });
  81.  
  82.         i += 1;
  83.         utmDate.setDate(utmDate.getDate() + 1);
  84.     }
  85. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement