save the goddamn signing key

save the signing key on FIMFiction

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        save the goddamn signing key
// @namespace   hax
// @include     http://www.fimfiction.net/*
// @version     2
// @grant       GM_getValue
// @grant       GM_setValue
// @description save the signing key on FIMFiction
// ==/UserScript==


function restore() {
  if(!localStorage.signing_key) {
    var testkey = GM_getValue('signing_key');
    if(testkey) {
      localStorage.signing_key = testkey;
      return;
    }
    testkey = localStorage.backup_signing_key;
    if(testkey) {
      localStorage.signing_key = testkey;  
      return;
    }
  }  
}

var saver = null;
var saveInterval = 100;

function save() {
  console.log('save',saveInterval);
  if(localStorage.signing_key && localStorage.signing_key != GM_getValue('signing_key')) {
    console.log("Setting the signing key to",localStorage.signing_key);
    GM_setValue('signing_key',localStorage.signing_key);
    localStorage.backup_signing_key = localStorage.signing_key;
    // no need to continue checking, it only sets the signing key once per page load at most
  } else {
    // taper off because it doesn't set any signing key after it's done futzing around
    saveInterval *= 4;
    saver = setTimeout(save,saveInterval);
  }
}


document.addEventListener('DOMContentLoaded',function(e) { 
  if(!saver) {
    // this could get called for sub-page loads so just start it up once
    saver = setTimeout(save,saveInterval); // amazingly, the site erases the signing key, then sets the signing key, then erases it and tells you to log in again.
    restore(); // if we restore from backup it'll overwrite that later anyway, so we can do this early.
  }
},true);