save the goddamn signing key

save the signing key on FIMFiction

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);