Fanfiction.net Unwanted Result Filter

Make up for how limited Fanfiction.net's result filtering is

< Feedback on Fanfiction.net Unwanted Result Filter

Question/comment

§
Posted: 2016-04-04

Quick hack to (if I've got it right) filter by a word list.

--- old.js  2016-04-04 21:18:29.435722300 +1000
+++ new.js  2016-04-04 21:18:20.030711000 +1000
@@ -67,6 +67,37 @@
       'size': 255,
       'default': ".*(fem|not?[ ]+)(slash|yaoi)([.,!: ]|$)"
     },
+    'filter_words': {
+      'section': ['Unwanted word Filter',
+        'Hide unwanted words in author pages and "All Crossovers" searches'],
+        'label': 'Enabled',
+        'labelPos': 'right',
+        'type': 'checkbox',
+        'default': true,
+    },
+    'hide_words': {
+        'label': 'No Placeholder',
+        'labelPos': 'right',
+        'type': 'checkbox',
+        'default': false,
+    },
+    'unwanted_words': {
+      'label': 'Unwanted words (One per line, blank lines and lines ' +
+        'beginning with # will be ignored):',
+        'type': 'textarea',
+        'size': 100,
+        'default': [
+          "drabble",
+        ].join("\n"),
+    },
+    'unwanted_words_escape': {
+      'label': 'Lines are literal strings (uncheck for regular expressions)',
+      'labelPos': 'right',
+      'title': 'NOTE: Leading/trailing whitespace is always ignored and ' +
+               'newlines always have OR behaviour.',
+      'type': 'checkbox',
+      'default': true,
+    },
     'filter_cats': {
       'section': ['Unwanted Category Filter',
         'Hide unwanted fandoms in author pages and "All Crossovers" searches'],
@@ -177,10 +208,13 @@
       "#ffnet_result_filter_filter_manual_var, " +
       "#ffnet_result_filter_filter_slash_var, " +
       "#ffnet_result_filter_filter_cats_var, " +
+      "#ffnet_result_filter_filter_words_var, " +
       "#ffnet_result_filter_hide_manual_var, " +
       "#ffnet_result_filter_hide_slash_var, " +
       "#ffnet_result_filter_hide_cats_var " +
+      "#ffnet_result_filter_hide_words_var " +
       "    { display: inline-block; margin-right: 1em !important; } " +
+      "#ffnet_result_filter_field_unwanted_words," +
       "#ffnet_result_filter_field_unwanted_cats { min-height: 10em; }"
       ].join('\n#ffnet_result_filter ')),
     'events': {
@@ -256,6 +290,14 @@
     return cats_out;
   };

+  /// Parse a usable list of word patterns from a raw string
+  var parse_words_list = function(s, escape) {
+    // Parse the config
+    var words_raw = parse_lines(s);
+    if (escape) { words_raw = words_raw.map(re_escape) }
+    return words_out;
+  };
+
   // Parse a usable list of story IDs from a raw string
   var parse_id_list = function(s) {
     return parse_lines(s).filter(rows_filter);
@@ -293,6 +335,11 @@
       GM_config.get('unwanted_cats_commute')
     );

+    var bad_words = parse_words_list(
+      GM_config.get('unwanted_words'),
+      GM_config.get('unwanted_words_escape')
+    );
+
     if (GM_config.get('filter_manual')) {
       var manual_story_ids = parse_id_list(GM_config.get('unwanted_manual'));
     } else {
@@ -305,6 +352,7 @@
     var not_slash_re = new RegExp(GM_config.get('not_slash_pat'), 'i');
     var cats_re = new RegExp("(?:^|- )(.*(" + bad_cats.join('|') + ').*) - Rated:.*');
     var cat_link_re = new RegExp(bad_cats.join('|'));
+    var words_re = new RegExp(bad_words.join('|'), 'i');

     // Clean up after any previous run
     $(".filter_placeholder").remove();
@@ -336,6 +384,18 @@
             hide_entry(story);
           } else {
             add_placeholder(story, matches[1]);
+          }
+          continue;
+        }
+      }
+
+      if (GM_config.get('filter_words')) {
+        var matches = description.match(words_re);
+        if (matches && matches.length > 0) {
+          if (GM_config.get('hide_words')) {
+            hide_entry(story);
+          } else {
+            add_placeholder(story, matches[1]);
           }
           continue;
         }

ssokolowAuthor
§
Posted: 2016-04-04
Edited: 2016-04-04

Thanks. The script's innards are already getting a bit unwieldy, so I'll probably refactor that code to reduce duplication before I merge it, but it does look correct at first glance.

I was actually thinking about description filtering so things like "HP/DM" (Harry Potter - Draco Malfoy pairing) could be filtered out.

§
Posted: 2018-06-29

where would I patch this code in?

ssokolowAuthor
§
Posted: 2018-06-29

It's in diff format so, unless I changed the source too much since then, the UNIX patch tool (or anything else compatible) should figure that out for you if you name the script old.js (see line 1) and then apply the patch against it.

I'll see if I can find time tomorrow to get you a better answer.

ssokolowAuthor
§
Posted: 2018-06-30

OK, today didn't go as expected and I've been having trouble finding a nice, simple GUI for applying diff patches on Windows, but I can at least explain how to apply one of these manually.

A unified diff file is broken up into a series of chunks, each with a @@ line at the top. (In this case, six chunks.)

Inside a chunk, lines prefixed with + should be added and lines starting with - should be removed.

So, for example, this chunk...

@@ -305,6 +352,7 @@
     var not_slash_re = new RegExp(GM_config.get('not_slash_pat'), 'i');
     var cats_re = new RegExp("(?:^|- )(.*(" + bad_cats.join('|') + ').*) - Rated:.*');
     var cat_link_re = new RegExp(bad_cats.join('|'));
+    var words_re = new RegExp(bad_words.join('|'), 'i');

     // Clean up after any previous run
     $(".filter_placeholder").remove();

says to find this in the file (which should be near line 305 according to the @@ line)...

    var not_slash_re = new RegExp(GM_config.get('not_slash_pat'), 'i');
    var cats_re = new RegExp("(?:^|- )(.*(" + bad_cats.join('|') + ').*) - Rated:.*');
    var cat_link_re = new RegExp(bad_cats.join('|'));

    // Clean up after any previous run
    $(".filter_placeholder").remove();

...and turn it into this:

    var not_slash_re = new RegExp(GM_config.get('not_slash_pat'), 'i');
    var cats_re = new RegExp("(?:^|- )(.*(" + bad_cats.join('|') + ').*) - Rated:.*');
    var cat_link_re = new RegExp(bad_cats.join('|'));
    var words_re = new RegExp(bad_words.join('|'), 'i');

    // Clean up after any previous run
    $(".filter_placeholder").remove();

That is, add the var words_re line after the var cat_link_re line.

Post reply

Sign in to post a reply.