Skip to content

Instantly share code, notes, and snippets.

@njt1982
Created January 30, 2014 10:22
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 njt1982/8705893 to your computer and use it in GitHub Desktop.
Save njt1982/8705893 to your computer and use it in GitHub Desktop.
TamperMonkey script to detect Varnish hits
// ==UserScript==
// @name Varnish Header Checker
// @namespace http://www.thingy-ma-jig.co.uk/
// @version 0.1
// @description Check for the presence of a Varnish Header and put an alert DIV on the page.
// @match http://*/*
// @copyright 2014+, You
// ==/UserScript==
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
console.log(headers);
var baseCSS = 'position:fixed;opacity:0.5;top:32px;left:32px;width:128px;height:32px;line-height:32px;padding:4px;z-index:999';
if (headers.indexOf('x-varnish-cache: miss') != -1) {
// create almost any element this way...
var el = document.createElement("div");
// add some text to the element...
el.innerHTML = "VARNISH MISS";
el.style.cssText = baseCSS + ';border:1px solid #800;background-color:#f88;color:#FFF;';
// "document.body" can be any element on the page.
document.body.appendChild(el);
}
else if (headers.indexOf('x-varnish-cache: hit') != -1) {
// create almost any element this way...
var el = document.createElement("div");
// add some text to the element...
el.innerHTML = "VARNISH HIT";
el.style.cssText = baseCSS + ';border:1px solid #080;background:#8f8;color:#000;';
// "document.body" can be any element on the page.
document.body.appendChild(el);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment