From 21aace3c33126b1788ea53f4415f9ba89b4d3b7a Mon Sep 17 00:00:00 2001 From: Robert Rudman Date: Wed, 10 Oct 2018 10:03:09 +1100 Subject: [PATCH] Expose hideOneboxes function. Add functionality to re-hide onebox --- NoOneboxesInChatTranscripts.user.js | 53 +++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/NoOneboxesInChatTranscripts.user.js b/NoOneboxesInChatTranscripts.user.js index d343dd71..8d368718 100644 --- a/NoOneboxesInChatTranscripts.user.js +++ b/NoOneboxesInChatTranscripts.user.js @@ -3,7 +3,7 @@ // @description Collapses oneboxes from chat transcripts, click to display onebox // @homepage https://github.com/samliew/SO-mod-userscripts // @author @samliew -// @version 1.1 +// @version 1.2 // // @include https://chat.stackoverflow.com/transcript/* // @include https://chat.stackexchange.com/transcript/* @@ -18,19 +18,52 @@ 'use strict'; - function doPageload() { + /* Call hideOneboxes() from other scripts to hide all new oneboxes on any page update + * Params: + * mid : message ID to re-hide its expanded onebox, OR + * -1 to re-hide all expanded oneboxes + */ + unsafeWindow.hideOneboxes = function(mid = null) { - $('.onebox').hide().each(function() { + // Display original link and hide oneboxes who hasn't been hidden before + $('.onebox').not('.js-onebox-hidden').addClass('js-onebox-hidden').hide().each(function() { + + // Onebox permalink is usually the first URL in the onebox let url = $(this).find('a').first().attr('href'); + + // If onebox type is a tweet, permalink is the last link in onebox if($(this).hasClass('ob-tweet')) url = $(this).find('a').last().attr('href'); - $(`${url}`) + const loadOneboxText = 'click to load onebox'; + const hideOneboxText = 'click to hide onebox'; + let isVisible = false; + // Click placeholder to show onebox + $(`${url}`) .click(function() { - $(this).hide().next().show(); + isVisible = !isVisible; + if (isVisible) { + $(this).addClass('js-show-onebox'); + $(this).attr('title', hideOneboxText); + } else { + $(this).removeClass('js-show-onebox'); + $(this).attr('title', loadOneboxText); + } }).insertBefore(this); + + // Also collapse user signature (use tiny-signature) + $(this).parents('.monologue').find('.tiny-signature').fadeIn(200).siblings().hide(); }); - } + // Re-hide oneboxes if mid is set + if(mid === -1) { + // Re-hide all expanded oneboxes + $('.js-show-onebox').removeClass('js-show-onebox'); + } + else if(mid) { + // Re-hide specific message's onebox + $(`#message-${mid}`).find('.js-show-onebox').removeClass('js-show-onebox'); + } + }; function appendStyles() { @@ -41,6 +74,12 @@ border-left: 3px solid orange; cursor: zoom-in; } +.js-show-onebox { + cursor: zoom-out; +} +.js-show-onebox + .js-onebox-hidden { + display: block !important; +} `; $('body').append(styles); @@ -49,6 +88,6 @@ // On page load appendStyles(); - doPageload(); + hideOneboxes(); })();