Created
January 30, 2014 10:22
-
-
Save njt1982/8705893 to your computer and use it in GitHub Desktop.
TamperMonkey script to detect Varnish hits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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