Bulk delete

Delete multiple items in your database

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

Advertisement:

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

Advertisement:

// ==UserScript==
// @name           Bulk delete
// @description    Delete multiple items in your database
// @match          https://*.memrise.com/course/*/*/edit/database/*
// @run-at         document-end
// @version        1.0.0
// @grant          none
// @namespace      https://greasyfork.org/users/213706
// @license        MIT
// ==/UserScript==

/* jshint esversion:6 */

function main() {

  var BulkEditer = {
    isOn: false,
    
    /**
     * Entrypoint
     */
    init: function() {
			var $form = $('#content .pool-header .pull-right form').last();

      this.addBtn($form);
    },

    /**
     * Add btn "Bulk delete" to the button list
     * @param element $form
     */
    addBtn: function($form) {
      if(!$form) {
        return;
      }

      var btn = document.createElement('button');
      btn.setAttribute('type', 'button');
      btn.setAttribute('class', 'btn btn-icos-active bulk-delete');
      btn.innerHTML = '<i class="ico ico-plus"></i>Bulk delete';

      setTimeout(function(){
        var $firstBtn = $('.btn', $form).first();

        $(btn).insertBefore($firstBtn);

        btn.addEventListener('click', this.handleClickBtn.bind(this));
      }.bind(this), 0);
    },

    /**
     * When "Bulk delete" is clicked,
     * either display the checkboxes or delete all lines that are checked
     */
    handleClickBtn: function() {
      BulkEditer.isOn = !BulkEditer.isOn;

      if (BulkEditer.isOn) {
        BulkEditer.showCheckboxes();
      } else {
        BulkEditer.handleDelete();
      }
    },

    /**
     * Display checkbox at the beggining of each row
     */
    showCheckboxes: function() {
      $('tr.thing td:first-child').each(function(){
        var html = `<input
          class="bulk-delete"
          type="checkbox"
          title="Select word"
        />`;
        $(this).prepend(html);
      });
    },

    /**
     * Retrieve all lines with checked checkboxes
     * Trigger line deletion
     * And remove all checkboxes
     */
    handleDelete: function() {
      $('.bulk-delete:checked').each(function(){
        var $tr = $(this).closest('tr');

      	BulkEditer.deleteItem($tr);
      });
      $('tr input.bulk-delete').remove();
    },

    /**
     * Delete the given row
     */
    deleteItem: function($tr) {
      if (!$tr) {
      	return;
      }
      var thingId = $tr.data('thing-id');

      $.ajax('/ajax/thing/delete/', {
        method: 'POST',
        data: {
        	thing_id: thingId,
        },
        xhrFields: {
           withCredentials: true
        },
      }).done(function(){
      	$tr.remove();
      });
    },
  };

  BulkEditer.init();
}


// Add CSS
var css = document.createElement('style');
css.textContent = `
    .bulk-delete {
      min-width: unset !important;
    }
    .bulk-delete + .ico-close {
    	display: none !important;
    }`;
document.head.appendChild(css);

// Inject JS directly in page to prevent limitations of access
var script = document.createElement('script');

script.setAttribute("type", "application/javascript");
script.appendChild(document.createTextNode('('+ main +')();'));
document.body.appendChild(script);