Adds ISO functionality to Serenes Forest (and other new IPB sites).
// ==UserScript== | |
// @name Serenes Forest - New ISOs | |
// @namespace Makaze | |
// @description Adds an ISO option for new SF | |
// @include *serenesforest.net/forums/* | |
// @grant none | |
// @version 2.0.3 | |
// ==/UserScript== | |
function createElement(type, callback) { | |
var element = document.createElement(type); | |
callback(element); | |
return element; | |
} | |
function runInJQuery(code) { | |
/*if (typeof(jQuery) == "undefined") { | |
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)); | |
})); | |
}; | |
})); | |
} else {*/ | |
document.body.appendChild(createElement('script', function(content) { | |
content.appendChild(document.createTextNode(code)); | |
})); | |
/*}*/ | |
} | |
function ThreadCrawler(threadID) { | |
this.threadID = location.href.match(/topic\/([^\/]+)/)[1]; | |
var $self = this; | |
this.users = {}; | |
this.posts = []; | |
this.crawling = false; | |
this.endPage = 0; | |
this.currPage = 1; | |
if (location.href.match(/page=\d+/)) { | |
this.currPage = parseInt(location.href.match(/page=(\d+)/)[1]); | |
} | |
jQuery('body').append('<div class="ThreadCrawler">'); | |
jQuery('.ThreadCrawler').hide(); | |
if (this.crawling) { | |
this.loadPage(); | |
} | |
jQuery('.ipsToolList').first().append('<li class="ipsResponsive_hidePhone"><a href="javascript:void(0)" class="getISOs ipsButton ipsButton_link ipsButton_medium ipsButton_fullWidth" title="Run an ISO search using this page">Get ISOs (Page ' + $self.currPage + '+)</a></li> '); | |
jQuery('.getISOs').on('click', function(event) { | |
event.preventDefault(); | |
$self.endPage = parseInt(prompt('What page do you want to end your ISOs? Enter 0 to go to the end.', '0')); | |
$self.loadPage(); | |
jQuery(event.target).parent('li').hide(); | |
}); | |
jQuery('.cTopic').on('click', '.cMultiQuote', function(event) { | |
event.preventDefault(); | |
var $self = jQuery(this); | |
var _buildMultiQuote = function(count) { | |
var quoterElem = jQuery('#ipsMultiQuoter'); | |
if (!quoterElem.length && count) { | |
ips.getContainer().append(ips.templates.render('core.posts.multiQuoter', { | |
count: ips.getString('multiquote_count', { | |
count: ips.pluralize(ips.getString('multiquote_count_plural'), [count]) | |
}) | |
})); | |
ips.utils.anim.go('zoomIn fast', jQuery('#ipsMultiQuoter')); | |
} else { | |
quoterElem.find('[data-role="quotingTotal"]').text(ips.pluralize(ips.getString('multiquote_count_plural'), [count])); | |
if (count && quoterElem.is(':visible')) { | |
ips.utils.anim.go('pulseOnce fast', quoterElem); | |
} else if (count && !quoterElem.is(':visible')) { | |
ips.utils.anim.go('zoomIn fast', quoterElem); | |
} else { | |
ips.utils.anim.go('zoomOut fast', quoterElem); | |
} | |
} | |
}; | |
var _prepareQuote = function(html) { | |
if (html.find('blockquote.ipsQuote') && html.find('blockquote.ipsQuote').parent() && html.find('blockquote.ipsQuote').parent().get(0) && html.find('blockquote.ipsQuote').parent().get(0).tagName === 'DIV' && html.find('blockquote.ipsQuote').siblings().length === 0) { | |
var div = html.find('blockquote.ipsQuote').closest('div'); | |
div.next('p').find("br:first-child").remove(); | |
div.remove(); | |
} else { | |
html.find('blockquote.ipsQuote').remove(); | |
} | |
html.find('.ipsStyle_spoilerFancy,.ipsStyle_spoiler').replaceWith(ips.templates.render('core.posts.quotedSpoiler')); | |
html.find("[data-excludequote]").remove(); | |
html.find('.ipsQuote_citation').remove(); | |
html.find('[data-quote-value]').each(function() { | |
jQuery(this).replaceWith('<p>' + jQuery(this).attr('data-quote-value') + '</p>'); | |
}); | |
return html; | |
}; | |
(function(event) { | |
var mainData = jQuery($self).parents('.ipsComment_content').first().data('quotedata'); | |
var button = jQuery(event.currentTarget); | |
var mqActive = button.attr('data-mqActive'); | |
var html = _prepareQuote(jQuery('<div/>').html($self.parents('.cPost').find('[data-role="commentContent"]').html())); | |
var fullData = { | |
userid: mainData.userid, | |
username: mainData.username, | |
timestamp: mainData.timestamp, | |
contentapp: mainData.contentapp, | |
contenttype: mainData.contenttype, | |
contentclass: mainData.contentclass, | |
contentid: mainData.contentid, | |
contentcommentid: mainData.contentcommentid, | |
quoteHtml: html.html(), | |
button: button.attr('data-mqId') | |
}; | |
if (mqActive) { | |
(function(data) { | |
var mqData = ips.utils.db.get('mq', 'data'); | |
var key = data.contentapp + '-' + data.contenttype + '-' + data.contentcommentid; | |
if (mqData[key] != null) { | |
mqData = _.omit(mqData, key); | |
ips.utils.db.set('mq', 'data', mqData); | |
_buildMultiQuote(Object.keys(mqData).length); | |
} | |
})(fullData); | |
button.removeClass('ipsButton_alternate').addClass('ipsButton_simple').removeAttr('data-mqActive').html(ips.templates.render('core.posts.multiQuoteOff')); | |
} else { | |
(function(data) { | |
var mqData = ips.utils.db.get('mq', 'data'); | |
var key = data.contentapp + '-' + data.contenttype + '-' + data.contentcommentid; | |
if (Object.keys(mqData).length == 50) { | |
ips.ui.alert.show({ | |
type: 'alert', | |
icon: 'warn', | |
message: ips.pluralize(ips.getString('maxmultiquote'), 50), | |
callbacks: { | |
ok: function() { | |
$("button[data-mqId='" + data.button + "']").removeClass('ipsButton_alternate').addClass('ipsButton_simple').removeAttr('data-mqActive').html(ips.templates.render('core.posts.multiQuoteOff')); | |
} | |
} | |
}); | |
return false; | |
} | |
mqData[key] = data; | |
ips.utils.db.set('mq', 'data', mqData); | |
_buildMultiQuote(Object.keys(mqData).length); | |
})(fullData); | |
button.removeClass('ipsButton_simple').addClass('ipsButton_alternate').attr('data-mqActive', true).html(ips.templates.render('core.posts.multiQuoteOn')); | |
} | |
})(event); | |
}); | |
this.loadPage = function() { | |
var $self = this; | |
if (!jQuery('.ThreadCrawlSplash').length) { | |
jQuery('.ipsToolList').first().parent().append('<div class="ThreadCrawlSplash" style=" padding: 10px; box-sizing: border-box; background-color: white; border-radius: 5px; "><img src="https://i.imgur.com/CrFfGbd.gif" style=" height: 100px; "> <span>Loading Page ' + $self.currPage + '...</span></div>'); | |
} | |
jQuery('.ThreadCrawlSplash').find('span').text('Loading Page ' + $self.currPage + '...'); | |
jQuery('div.ThreadCrawler').load(location.href.match(/^.*?topic\/([^\/]+)\//)[0] + '&page=' + $self.currPage + ' .cTopic', function() { | |
jQuery('div.ThreadCrawler').find('.cPost').each(function(index, el) { | |
var currPost = jQuery(el), | |
currPostNumber = 1 + (($self.currPage - 1) * 25) + (parseInt(jQuery('div.ThreadCrawler .cPost').index(el))), | |
currAuthorID = currPost.find('.cAuthorPane_author a').attr('href').match(/profile\/(\d+)/)[1], | |
currAuthorMeta = currPost.find('.ipsComment_author.cAuthorPane')[0].innerHTML, | |
currPostMeta, | |
currPostContent = currPost.find('.ipsComment_content')[0].outerHTML; | |
currPost.find('[data-role="shareComment"]').text('#' + currPostNumber); | |
currPost.find('.cMultiQuote').removeClass('ipsHide'); | |
currPost.find('[data-action="quoteComment"]').parent().addClass('ipsHide'); | |
currPost.find('[data-role="memberSignature"]').remove(); | |
currPostContent = currPost.find('.ipsComment_content')[0].outerHTML; | |
currPostMeta = currPost.find('.ipsComment_meta')[0].innerHTML; | |
if (!$self.users.hasOwnProperty(currAuthorID)) { | |
$self.users[currAuthorID] = currAuthorMeta; | |
} | |
currPost.addClass('postBy_' + currAuthorID).addClass('ThreadCrawler_post'); | |
$self.posts[currPostNumber] = { | |
'author': currAuthorID, | |
'meta': currPostMeta, | |
'content': currPostContent | |
}; | |
}); | |
if ((jQuery('div.ThreadCrawler').find('.ipsPagination_next').not('.ipsPagination_inactive').length > 0 && $self.endPage === 0) || ($self.currPage < $self.endPage)) { | |
$self.currPage++; | |
$self.loadPage(); | |
} else { | |
jQuery('.ThreadCrawlSplash').remove(); | |
jQuery('.getISOs') | |
$self.getAuthors(); | |
jQuery('.getISOs').text('Update ISOs (' + $self.currPage + '+)'); | |
jQuery('.getISOs').parent('li').show(); | |
if (document.title.indexOf("[ISOs]" < 0)) { | |
document.title = "[ISOs] " + document.title; | |
} | |
} | |
}); | |
}; | |
this.showAuthors = function(authors) { | |
var $self = this, | |
mainTopic = jQuery('[data-role="commentFeed"]').first(), | |
i = 0, | |
pages, | |
filteredList = $self.posts.filter(function(item, index) { | |
var j = 0; | |
for (j = 0; j < authors.length; j++) { | |
if (item['author'] == authors[j]) { | |
return true; | |
} | |
} | |
return false; | |
}); | |
filteredList.sort(function(a, b) { | |
var aNum = parseInt(a['meta'].match(/shareComment">#(\d+)</)[1]), | |
bNum = parseInt(b['meta'].match(/shareComment">#(\d+)</)[1]); | |
if (aNum > bNum) { | |
return 1; | |
} else if (aNum < bNum) { | |
return -1; | |
} else { | |
return 0; | |
} | |
}); | |
console.log(filteredList); | |
mainTopic.empty(); | |
jQuery(filteredList).each(function(index, item) { | |
mainTopic.append('<article itemscope="" itemtype="http://schema.org/Comment" class="cPost ipsBox ipsComment ipsComment_parent ipsClearfix ipsClear ipsColumns ipsColumns_noSpacing ipsColumns_collapsePhone"><aside class="ipsComment_author cAuthorPane ipsColumn ipsColumn_medium">' + $self.users[item['author']] + '</aside><div class="ipsColumn ipsColumn_fluid">' + item['content'] + '</div></article>'); | |
}); | |
pages = Math.ceil(mainTopic.find('.cPost').length / 25); | |
pages = (pages > 0) ? pages : 1; | |
jQuery('.ipsPagination').not('.authorList').find('li').not('.ipsPagination_page').hide(); | |
jQuery('.ipsPagination').not('.authorList').on('click', '.ipsPagination_page a', function(event) { | |
event.preventDefault(); | |
$self.paginate(parseInt(jQuery(event.target).data('page'))); | |
}); | |
jQuery('.ipsPagination').not('.authorList').empty(); | |
jQuery('.ipsPagination').not('.authorList').each(function(index, el) { | |
var currPagination = jQuery(el); | |
for (i = 0; i < pages; i++) { | |
currPagination.append('<li class="ipsPagination_page"><a href="#" data-page="' + (i + 1) + '">' + (i + 1) + '</a></li> '); | |
} | |
}); | |
$self.paginate(1); | |
var mqData = ips.utils.db.get('mq', 'data'); | |
mainTopic.find('.cPost .cMultiQuote').each(function() { | |
var mainData = jQuery(this).parents('.ipsComment_content').first().data('quotedata'); | |
var button = jQuery(this); | |
var mqActive = mqData.hasOwnProperty(mainData.contentapp + '-' + mainData.contenttype + '-' + mainData.contentcommentid); | |
if (!mqActive) { | |
button.removeClass('ipsButton_alternate').addClass('ipsButton_simple').removeAttr('data-mqActive').html(ips.templates.render('core.posts.multiQuoteOff')); | |
} else { | |
button.removeClass('ipsButton_simple').addClass('ipsButton_alternate').attr('data-mqActive', true).html(ips.templates.render('core.posts.multiQuoteOn')); | |
} | |
}); | |
}; | |
this.paginate = function(pageNumber) { | |
var $self = this, | |
mainTopic = jQuery('[data-role="commentFeed"]').first(), | |
start = 25 * (pageNumber - 1), | |
end = start + 25; | |
jQuery('.ipsPagination').not('.authorList').find('.ipsPagination_page').removeClass('ipsPagination_active'); | |
jQuery('.ipsPagination').not('.authorList').find('.ipsPagination_page a[data-page="' + pageNumber + '"]').parent().addClass('ipsPagination_active'); | |
mainTopic.find('.cPost').hide(); | |
mainTopic.find('.cPost').slice(start, end).show(); | |
jQuery('html, body').animate({ | |
scrollTop: mainTopic.find('.cPost').slice(start, start + 1).first().offset().top - 5 | |
}, 100); | |
}; | |
this.getAuthors = function() { | |
var $self = this, | |
tempUsername, | |
k = 0; | |
jQuery('.authorList').remove(); | |
jQuery('.ipsToolList').first().parent().after('<ul class="ipsPagination authorList" style=" zoom: 120%; background-color: white; display: block; border-radius: 3px; padding: 1em;"><span style=" margin-right: .5em; font-weight: bold;">Users: </span> </ul>'); | |
for (k in $self.users) { | |
if ($self.users.hasOwnProperty(k)) { | |
tempUsername = jQuery($self.users[k]).find('[itemprop="name"]').text().trim(); | |
jQuery('.authorList').append('<li class="ipsPagination_page"><a href="javascript:void(0)" data-page="' + k + '">' + tempUsername + '</a></li> '); | |
} | |
} | |
jQuery('.authorList').on('click', '.ipsPagination_page a', function(event) { | |
var authorsArray = []; | |
event.preventDefault(); | |
jQuery(event.target).parents('.ipsPagination_page').toggleClass('ipsPagination_active'); | |
jQuery('.authorList .ipsPagination_active').each(function(index, el) { | |
var curr = jQuery(el); | |
authorsArray.push(parseInt(curr.find('a').data('page'))); | |
}); | |
$self.showAuthors(authorsArray); | |
}); | |
}; | |
} | |
function main() { | |
if (window.self === window.top && window.location.href.match(/topic\/([^\/]+)/)[1]) { | |
setTimeout(function() { | |
ISO = new ThreadCrawler(); | |
}, 500); | |
} | |
} | |
runInJQuery(createElement.toString() + ThreadCrawler.toString() + main.toString() + ';main();'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Version 2.0 features support for Multi-Quoting from the ISOs.