Skip to content

Commit

Permalink
Expose hideOneboxes function. Add functionality to re-hide onebox
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudman committed Oct 9, 2018
1 parent fc00e8e commit 21aace3
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions NoOneboxesInChatTranscripts.user.js
Expand Up @@ -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/*
Expand All @@ -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');

$(`<span class="has-onebox" title="click to load onebox">${url}</span>`)
const loadOneboxText = 'click to load onebox';
const hideOneboxText = 'click to hide onebox';
let isVisible = false;
// Click placeholder to show onebox
$(`<span class="has-onebox" title="${loadOneboxText}">${url}</span>`)
.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() {

Expand All @@ -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;
}
</style>
`;
$('body').append(styles);
Expand All @@ -49,6 +88,6 @@

// On page load
appendStyles();
doPageload();
hideOneboxes();

})();

0 comments on commit 21aace3

Please sign in to comment.