LongMsgBuster

Collapse or shrink tall messages

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ć!)

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ć!)

// ==UserScript==
// @name        LongMsgBuster
// @namespace   https://gitter.im/
// @description Collapse or shrink tall messages
// @include     https://gitter.im/*/*/~*#*
// @version     3
// @grant       none
// ==/UserScript==

var MAX_HEIGHT = 350; // Pixels
var WARNING = '<p>This message was hidden because it exceeded the desired height.</p>';

document.addEventListener('keydown', function(e) {
  // Key Binding | Defaults:
  // CTRL~CMD + ALT + F -> Shrink blocks
  // CTRL~CMD + ALT + G -> Collapse blocks
  var collapse = e.keyCode === 71; // G
  var shrink = e.keyCode === 70; // F
  var middle = e.altKey;
  var first = e.metaKey || e.ctrlKey;

  if (first && middle && shrink) {
    applyToDomElements(
      document.querySelectorAll('.chat-item'),
      shrinkChatBlock
    );
  } else if (first && middle && collapse) {
    applyToDomElements(
      document.querySelectorAll('.chat-item'),
      hideChatBlock
    );
  }
});

function applyToDomElements(elements, action) {
  for (var i = 0, l = elements.length; i < l; i++) {
    if (elements[i].offsetHeight > MAX_HEIGHT) {
      action(elements[i]);
    }
  }
}

function shrinkChatBlock(block) {
  var btn = document.createElement('button');
  var text = document.createTextNode('Expand');

  btn.appendChild(text);
  btn.addEventListener('click', function(e) {
    var block = e.target.parentNode;

    block.style.textAlign = 'left';
    block.innerHTML = this.oldHTML;
    block.style.boxShadow = 'none';
  }.bind({ oldHTML: block.innerHTML }));

  block.innerHTML = WARNING;
  block.style.boxShadow = '0px 1px 5px 1px rgba(0,0,0,0.25)';
  block.style.textAlign = 'center';
  btn.appendChild(text);
  block.appendChild(btn, block.firstChild);
}

function hideChatBlock(block) {
  // Straight up delete the node from the tree
  block.parentNode.removeChild(block);
}