0

I have a script that goes as follows:

// ==UserScript==
// @name     TempleOS Ignorer with added Braed Ignoring
// @include     *://v3rmillion.net/*
// @grant    none
// ==/UserScript==

const ignore = ['189822','695729', '1797404', '1439', '1290050', '941293', '1696676', '440792', '1391811', '114505']

new Array(...document.getElementsByClassName('author'))
   .filter(author => ignore.includes(author.firstElementChild.getAttribute('href').match(/=([0-9]+)/)[1]))
   .forEach(author => author.parentElement.parentElement.parentElement.remove())

new Array(...document.getElementsByClassName('author_information'))
   .filter(author => ignore.includes(author.firstElementChild.firstElementChild.firstElementChild.getAttribute('href').match(/=([0-9]+)/)[1]))
   .forEach(author => author.parentElement.parentElement.remove())

There's a div called post-head and I want to add a button to it that takes the user's ID and adds it to the ignore const. I'm fairly new to using Tampermonkey, most of this was done by someone else and I want to make it easier to add users to this ignore list. Something like this. (Very sloppily done, but you get the idea.)

double-beep
  • 5,031
  • 17
  • 33
  • 41
Despairity
  • 13
  • 1
  • 1
    Well to start off, if that is the entire code you have, then clearly it isn't creating any `Buttons`. There are plenty of user script tutorials out there. [Here is a post on this site about how to add a button to a site using JavaScript](https://stackoverflow.com/questions/6480082/add-a-javascript-button-using-greasemonkey-or-tampermonkey). Once you have the `Button` created, you probably will want to tie it to the users ID/author id, and then `ignore.push(author_id)` on the `Button` `click` event to push the user id to the `const ignore`. – treckstar Jun 24 '22 at 09:20
  • 1
    It isn't creating any buttons as in its unable, or it simply isn't creating buttons on its own? The idea was to add the functionality to the script to avoid tediously taking a member's ID and adding it to the script manually. – Despairity Jun 24 '22 at 09:40
  • Check out answer I added for an explanation. There are tons of ways to do this, but the script you posted isn't creating any buttons on the page. But it certainly is do-able. – treckstar Jun 24 '22 at 11:07
  • I've taken a look and I believe I have a fundamental understanding of what's going on, thanks mostly to your self-supplied explanation. Obviously, copy-pasting the whole thing didn't work, within the console I pieced together certain elements and began to somewhat understand what I had to do. If I play with it longer, I'll probably get it down for sure, but I don't have too much time to do so at this minute. I appreciate everything, treckstar! I hope from here this will give me the result I desire, I'll mark your answer as correct. – Despairity Jun 24 '22 at 11:37
  • Thanks, [see the animated GIF on this link](https://treckstar.net/assets/images/2022-06-24-jquery-console-test2.gif) how I was able to copy **just** the `JavaScript` and paste it into the `Console`, and it then added the buttons to each post. But anyways hope this helps! – treckstar Jun 24 '22 at 12:17

1 Answers1

0

So you can try taking a look at this script I came up with real quick, and then you can try putting it into a UserScript if you'd like. I tried to keep this extremely simple and basic. Just know there are plenty of ways to accomplish this, and this isn't exactly the standard in 2022. Otherwise, you can just copy this JS code into the Dev Tools Console tab on the message board, and it should work for you.

Here is the overall summary of what this is:

  • HTML is just basic message board HTML, the button is added next at the bottom of the post on the left side
  • Loops through each .post element on the page, using jQuery .each(), get the idof the post, and the id of the author.
  • There are probably tons of ways to get the author/post id from the markup on this message board. I simply chose to split() the URL using = and grab the ID at the array index of [2]. Getting the id of the post was just replacing the post_ from the id element on the .post element.
  • Left in the console.log() so it can be tested, output an alert() notification for button onclick
  • Appended the markup, which was created using String Literals to the .author-controls elements. I used an <a> element instead of a <button> because I didn't want to add any CSS to make it match.

const ignore = [];

$('.post').each(function() {
  let US_pid = $(this).attr('id').replace('post_', '');
  console.log('id=post_' + US_pid);

  let US_author_id_array = $('.author_buttons > .postbit_find', this).attr('href').split('=')
  let US_author_id = US_author_id_array[2];
  console.log('author id: ' + US_author_id)

  let US_ignore_btn_markup = `<a href="#post_${US_pid}" class="author_ignore" data-author-id="${US_author_id}" data-post-id="${US_pid}" onclick="javascript: ignore.push(${US_author_id}); alert('Ignored: ${US_author_id}'); console.log(ignore);">Ignore User</a>`;

  console.log(US_ignore_btn_markup);

  $('.author_buttons', this).append(US_ignore_btn_markup);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="post " style="" id="post_7747511">
  <div class="post_head" style="padding-right:5px">

    <div class="float_right" style="vertical-align: middle">
      <strong><a class="postlink" href="showthread.php?pid=7747511#pid7747511" title=""></a></strong>
    </div>
  </div>
  <div class="post_author">
  </div>

  <div class="post_content">
  </div>
  <div class="post_controls">
    <div class="postbit_buttons author_buttons float_left">

      <a href="private.php?action=send&amp;uid=454420" title="Send this user a private message" class="postbit_pm"><span>PM</span></a>

      <a href="search.php?action=finduser&amp;uid=454420" title="Find all posts by this user" class="postbit_find"><span>Find</span></a>
    </div>
    <div class="postbit_buttons post_management_buttons float_right">

      <a href="javascript:Report.reportPost(7747511);" title="Report this post to a moderator" class="postbit_report"><span>Report</span></a>

    </div>
  </div>
</div>
treckstar
  • 1,956
  • 5
  • 21
  • 26