Fanfiction.net - Customize Default Result Filter

Override Fanfiction.net's default choices for result filters

< Feedback on Fanfiction.net - Customize Default Result Filter

Question/comment

§
Posted: 2017-12-19

This needs a new version.

Currently doesn't work due to the new versions of firefox having disabled 'GM_registerMenuCommand' and having turned 'GM_setValue' into a future (promise)

Unfortunately the api has several gotchas like 'await doesn't work at the top level'
info: https://gist.github.com/Rich-Harris/41e8ccc755ea232a5e7b88dee118bcf5
(thus the async functions)

and the menu api needs a manifest giving permission for that on the extension now. Since i have no idea how to do this myself, i posted this here.

Code that sort of does this is this:

// ==UserScript==
// @name Fanfiction.net - Customize Default Result Filter
// @namespace ssokolow.com
// @description Override Fanfiction.net's default choices for result filters
// @version 4
//
// @grant GM.getValue
// @grant GM.setValue
//
// @noframes
// @match *://www.fanfiction.net/*
// ==/UserScript==

// TODO: Try to find a way to safely URL-match so I can minimize the number
// of cases where I need to redirect and use @run-at document-start
// for the rest.

async function ffsetfilter() {
await GM.setValue('preferred_filter', window.location.search);
}


async function dofilter() {
var has_filters = (document.getElementById('filters') !== null);
// Skip everything if this isn't a relevant page since we can't URL match them.
if (has_filters) {
// Let short-circuit eval. only call GM_getValue once on empty query string
var preferred_filter;
if (window.location.search === "") {
preferred_filter = await GM.getValue('preferred_filter', 0);
}

if (preferred_filter) {
// Make it more clear when the page hasn't yet reloaded
document.querySelector('body').style = 'opacity: 0.2';
history.replaceState({}, '', preferred_filter);
location.reload();
} else {
//not sure this is needed anymore
// Work around bad interaction between Firefox and replaceState+reload
//var filter_form = document.querySelector('#filters #myform')
//if (filter_form) { filter_form.reset(); }
}
}
}
dofilter();
browser.menus.create({
id: "ffnet-filter",
title: "Save ffn filter",
documentUrlPatterns: ["*://www.fanfiction.net/*"],
contexts: ["page"]
});
browser.menus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "ffnet-filter" ) {
ffsetfilter()
}
);

ssokolowAuthor
§
Posted: 2017-12-19
Edited: 2017-12-19

Thanks. I haven't updated it yet because I'm still on Firefox 52ESR and Greasemonkey 3.x while I work on more urgent migrations.

(And I'm still not sure whether I'll move to 4.x or switch to a different userscript host for Firefox. It depends on whether there's a low-effort way to support both Greasemonkey 4.x and Chrome users.)

Oh, and the Chrome icon on this post is because I've got uMatrix randomizing my User Agent string.

ssokolowAuthor
§
Posted: 2018-09-05
Edited: 2018-09-05

New version finally released. Sorry for the crazy delay.

I just used gm4-polyfill, so Greasemonkey 4 users will see the menu entry showing up in the page's context menu now. (Given how much more verbose your version is than mine, you may want to see how I did it.)

EDIT: ...and I just turned off "Spoof Referer Header" in my uMatrix config for GreasyFork, so this should be the last time you see me as being on Chrome rather than Firefox in here.

ssokolowAuthor
§
Posted: 2018-09-05

...and there are now screenshots to show where the menu entry is in Greasemonkey 4 and Chrome+Tampermonkey.

Post reply

Sign in to post a reply.