Monitoring ViewState size

A short and very dirty script I wrote for TamperMonkey that keeps an eye on the size of your ViewState and alerts you if it gets a bit big. I appreciate this could be a lot better written feel free to take and adapt and improve – although let me know as well please!

Don’t forget to change the @match parameter

// ==UserScript==
// @name         Monitor ViewState size
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Monitors ViewState on page and alerts you if it gets big!
// @author       Rob Church
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser-polyfill.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser.min.js
// @match        http://*.yourdomain.com/*
// ==/UserScript==

/* jshint ignore:start */
var inline_src = (<><![CDATA[
/* jshint ignore:end */
/* jshint esnext: true */

    var allowedViewStateSize = 5000;
    var alerted = false;
    
    var viewStateSize = document.querySelector('#__VIEWSTATE').value.length;
    if (viewStateSize > allowedViewStateSize) {
       alert('Viewstate is large: ' + viewStateSize);
       alerted = true;
    }

    window.setInterval(function(){
       var viewStateSize = document.querySelector('#__VIEWSTATE').value.length;
       if ((viewStateSize > allowedViewStateSize) && (alerted == false)) {
          alert('Viewstate has *BECOME* large: ' + viewStateSize);
          alerted = true;
       }
    }, 2000);
    

/* jshint ignore:start */
]]></>).toString();
var c = babel.transform(inline_src);
eval(c.code);
/* jshint ignore:end */

Leave a Reply

Your email address will not be published. Required fields are marked *