save the goddamn signing key

save the signing key on FIMFiction

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);