Bulk delete

Delete multiple items in your database

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);