Skip to content

Instantly share code, notes, and snippets.

@BehindTheMath
Created May 8, 2017 23:56
Show Gist options
  • Select an option

  • Save BehindTheMath/dd2683e32257b4082def45066bad4bc2 to your computer and use it in GitHub Desktop.

Select an option

Save BehindTheMath/dd2683e32257b4082def45066bad4bc2 to your computer and use it in GitHub Desktop.
Show coupon codes directly on RetailMeNot without having to click or navigate to another page
// ==UserScript==
// @name Show Retailmenot codes
// @namespace behindthemath.io
// @include /.*retailmenot.com\/view\/[^\?]*$/
// @version 1
// @grant none
// ==/UserScript==
(function () {
var dataNewTab = document.querySelectorAll('a.button-show-code')[0].getAttribute('data-new-tab');
var url = "https://cors-anywhere.herokuapp.com/" + "https://retailmenot.com" + dataNewTab;
var headers = new Headers();
headers.append('Content-Type', 'text/html');
headers.append("Origin", "null");
var options = {
headers: headers
};
var request = new Request(url, options);
fetch(request)
.then(function (response) {
//debugger;
return response.text();
})
.then(function (text) {
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text, "text/html");
var buttonCodes = xmlDoc.querySelectorAll('a.button-code');
buttonCodes.forEach(function (currentNode, currentIndex, nodeList) {
var buttonShowCode = document.querySelector('a.button-show-code[data-new-tab="' + currentNode.getAttribute("data-new-tab") + '"]');
buttonShowCode.classList.remove("button-show-code");
buttonShowCode.classList.add("button-code");
buttonShowCode.textContent = currentNode.textContent;
});
})
.catch(function(err) {
console.log(err);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment