Skip to content

Instantly share code, notes, and snippets.

@Makaze
Last active April 19, 2021 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Makaze/4dbe005bbe745e662cab to your computer and use it in GitHub Desktop.
Save Makaze/4dbe005bbe745e662cab to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Two Plus Two - Expanded Search Results
// @namespace Makaze
// @description Shows expanded posts in search results instead of snippets.
// @include *://forumserver.twoplustwo.com/*
// @grant none
// @version 2.1.1
// ==/UserScript==
var MakazeScriptStyles,
styleElem;
function mqHeader() {
// Remove by value
function removeByValue(array, value) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && array.length) {
what = a[--L];
while ((ax = array.indexOf(what)) !== -1) {
array.splice(ax, 1);
}
}
return array;
}
// ###### MOZILLA COOKIE LIBRARY ######
/*\
|*|
|*| :: cookies.js ::
|*|
|*| A complete cookies reader/writer framework with full unicode support.
|*|
|*| Revision #1 - September 4, 2014
|*|
|*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
|*| https://developer.mozilla.org/User:fusionchess
|*|
|*| This framework is released under the GNU Public License, version 3 or later.
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*| Syntaxes:
|*|
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*| * docCookies.getItem(name)
|*| * docCookies.removeItem(name[, path[, domain]])
|*| * docCookies.hasItem(name)
|*| * docCookies.keys()
|*|
\*/
var docCookies = {
getItem: function (sKey) {
if (!sKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function (sKey, sPath, sDomain) {
if (!this.hasItem(sKey)) { return false; }
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey) {
if (!sKey) { return false; }
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: function () {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
return aKeys;
}
};
// ###### END MOZILLA COOKIE LIBRARY ######
function popCookie(name, value) {
var cookie = docCookies.getItem(name),
cookie_arr;
if (cookie) {
cookie_arr = (cookie && cookie.length) ? cookie.split(',') : [];
cookie_arr = removeByValue(cookie_arr, value);
if (cookie_arr.length) {
docCookies.setItem(name, cookie_arr.join(','), null, '/');
} else {
docCookies.removeItem(name, '/');
}
}
}
function pushCookie(name, value) {
var cookie = docCookies.getItem(name),
cookie_arr = (cookie && cookie.length) ? cookie.split(',') : [];
cookie_arr.push(value);
docCookies.setItem(name, cookie_arr.join(','), null, '/');
}
jQuery('table[id*=post]').each(function() {
var id = this.id.match(/\d+/)[0],
context = jQuery(this).find('> tbody > tr > td:last-of-type').not('.thead'),
source = (docCookies.getItem(name)) ? decodeURIComponent(docCookies.getItem(name)).split(',') : false;
jQuery(context).append('<div class="iso_buttons"><a class="iso_multiquote custom_multiquote" href="newreply.php?do=newreply&p=' + id + '" data-postid="' + id + '">MultiQuote (+)</a></div>');
});
jQuery('.custom_multiquote').each(function() {
var $self = jQuery(this),
$check = $self.find('img'),
name = 'vbulletin_multiquote',
post = $self.data('postid'),
source = (docCookies.getItem(name)) ? decodeURIComponent(docCookies.getItem(name)).split(',') : false;
if (source && jQuery.inArray(post.toString(), source) > -1) {
$self.text('MultiQuote (-)');
$self.css('font-style', 'italic');
}
});
jQuery('.custom_multiquote').on('click', function(event) {
var $self = jQuery(this),
$check = $self.find('img'),
name = 'vbulletin_multiquote',
post = $self.data('postid'),
source = (docCookies.getItem(name)) ? decodeURIComponent(docCookies.getItem(name)).split(',') : false;
event.preventDefault();
if (source && jQuery.inArray(post.toString(), source) > -1) {
popCookie(name, post);
$self.text('MultiQuote (+)');
$self.css('font-style', '');
} else {
pushCookie(name, post);
$self.text('MultiQuote (-)');
$self.css('font-style', 'italic');
}
});
}
function createElement(type, callback) {
var element = document.createElement(type);
callback(element);
return element;
}
function runInJQuery(code) {
document.body.appendChild(createElement('script', function(jQ) {
jQ.type = 'text/javascript';
jQ.src = 'https://code.jquery.com/jquery-2.1.3.min.js';
jQ.onload = function() {
document.body.appendChild(createElement('script', function(content) {
content.appendChild(document.createTextNode('jQuery.noConflict();' + code));
}));
};
}));
}
function expandResults() {
jQuery('table[id*=post]').each(function() {
var id = this.id.match(/\d+/)[0],
context = this.getElementsByTagName('em')[0].parentNode,
children = context.getElementsByTagName('em')[0].childNodes,
link = context.getElementsByTagName('em')[0].getElementsByTagName('a')[0];
jQuery(context).append(createElement('div', function(cont) {
cont.className = 'fullPost';
cont.id = 'fullPost' + id;
}));
while (children.length > 1) {
if (children[0].nodeType !== 1 || children[0].tagName !== 'A') {
children[0].remove();
} else {
children[1].remove();
}
}
context.getElementsByTagName('em')[0].style.display = 'block';
context.getElementsByTagName('em')[0].style.marginBottom = '1em';
jQuery(context).find('.fullPost').load('https://forumserver.twoplustwo.com/showpost.php?p=' + id + ' #post_message_' + id, function() {
jQuery('#fullPost' + id).fadeIn('slow');
});
});
}
if (document.getElementsByClassName('navbar')[0] != null && document.getElementsByClassName('navbar')[document.getElementsByClassName('navbar').length - 1].textContent.trim() === 'Search Results') {
//Styling
if (document.getElementById('MakazeScriptStyles') == null) {
MakazeScriptStyles = createElement('style', function(style) {
style.id = 'MakazeScriptStyles';
style.type = 'text/css';
});
document.head.appendChild(MakazeScriptStyles);
}
styleElem = document.getElementById('MakazeScriptStyles');
if (styleElem.hasChildNodes()) {
styleElem.childNodes[0].nodeValue += '\n\n';
} else {
styleElem.appendChild(document.createTextNode(''));
}
styleElem.childNodes[0].nodeValue +=
'.fullPost {\n' +
'display: none;\n' +
'}\n\n' +
'.fullPost, .fullPost * {\n' +
'font-size: 11px;\n' +
'}';
runInJQuery(
createElement.toString() +
expandResults.toString() +
'expandResults();' +
mqHeader.toString() +
'mqHeader();'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment