Interpals Conversations Delete All Button

Provides a convenience button to delete all conversations.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name Interpals Conversations Delete All Button
// @description Provides a convenience button to delete all conversations.
// @version 1
// @author Ouahib El Hanchi
// @include /^https?://www\.interpals\.net/pm\.php.*$/
// @run-at document-end
// @require http://code.jquery.com/jquery-latest.js
// @grant none
// @namespace https://greasyfork.org/users/346080
// ==/UserScript==

// Helpers ====================================================================

function delete_thread(thread_id, should_block_user=false) {
  console.log("Deleting thread id: " + thread_id);

  $.ajax({
    type : "POST",
    url : "/pm.php",
    data : {
      action : "delete_thread",
      thread : thread_id,
      block_user : should_block_user
    },
    dataType : "json"
  }).done(function(data) {
    if (data.success) {
      $("#thread_" + thread_id).remove();
      console.log("Thread(" + thread_id + "): Deleted successfully.");
    } else {
      if (data.error) {
        console_log("Thread(" + thread_id + "): " + data.error);
      } else {
        console.log("Thread(" + thread_id + "): Delete failed.");
      }
    }
  }).fail(function(data) {
    console.log("Thread(" + thread_id + "): Delete failed.");
  });
}

// ----------------------------------------------------------------------------

function delete_threads(event)
{  
  if(!confirm("Delete all conversations?")) {
    return;
  }
  
  var threads = document.querySelectorAll(".pm_thread");

  if(!threads || !threads.length) {
    console.log("There are no conversations.");
    return false;
  }
  
  threads.forEach(function(element) {
    var thread_id = element.getAttribute("id").replace("thread_", "");
    delete_thread(thread_id);
  });
}

// Entry-point ================================================================

function main()
{
    $("#controls").prepend(`
      <div style="border-left: 1px solid #ccc; float: right; line-height: 31px; padding: 0 5px;">
        <a id="delete-conversations" href="pm.php">
          <i class="fa fa-times fa-1"></i>
          Delete all conversations
        </a>
      </div>
    `);
    $("#delete-conversations").click(delete_threads);
}

// ----------------------------------------------------------------------------

main();

// END ========================================================================