2014-10-11 13:21:00 -04:00
|
|
|
// Thanks to Khorne on #8chan at irc.rizon.net
|
|
|
|
// https://gitlab.com/aymous/8chan-watchlist
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
/* jshint globalstrict:true, quotmark:single */
|
|
|
|
/* jshint browser:true, jquery:true, devel:true, unused:true, undef:true */
|
|
|
|
/* global active_page:false, board_name:false */
|
|
|
|
if(!localStorage.watchlist){
|
|
|
|
//If the watchlist is undefined in the localStorage,
|
|
|
|
//initialize it as an empty array.
|
|
|
|
localStorage.watchlist = '[]';
|
|
|
|
}
|
|
|
|
var watchlist = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [render /> Creates a watchlist container and populates it with info
|
|
|
|
* about each thread that's currently being watched. If the watchlist container
|
|
|
|
* already exists, it empties it out and repopulates it.]
|
|
|
|
* @param {[Bool]} reset [If true and the watchlist is rendered, remove it]
|
|
|
|
*/
|
|
|
|
watchlist.render = function(reset) {
|
|
|
|
/* jshint eqnull:true */
|
|
|
|
if (reset == null) reset = false;
|
|
|
|
/* jshint eqnull:false */
|
|
|
|
if (reset && $('#watchlist').length) $('#watchlist').remove();
|
|
|
|
var threads = [];
|
|
|
|
//Read the watchlist and create a new container for each thread.
|
|
|
|
JSON.parse(localStorage.watchlist).forEach(function(e, i) {
|
|
|
|
//look at line 69, that's what (e) is here.
|
|
|
|
threads.push('<div class="watchlist-inner" id="watchlist-'+i+'">' +
|
2014-10-19 22:30:59 -04:00
|
|
|
'<span>/'+e[0]+'/ - ' +
|
2015-03-20 04:09:21 -04:00
|
|
|
'<a href="'+e[3]+'">'+e[1].replace("thread_", _("Thread #"))+'</a>' +
|
2014-10-19 22:30:59 -04:00
|
|
|
' ('+e[2]+') </span>' +
|
|
|
|
'<a class="watchlist-remove">X</a>'+
|
2014-10-11 13:21:00 -04:00
|
|
|
'</div>');
|
|
|
|
});
|
|
|
|
if ($('#watchlist').length) {
|
|
|
|
//If the watchlist is already there, empty it and append the threads.
|
|
|
|
$('#watchlist').children('.watchlist-inner').remove();
|
|
|
|
$('#watchlist').append(threads.join(''));
|
|
|
|
} else {
|
|
|
|
//If the watchlist has not yet been rendered, create it.
|
2014-10-20 17:44:19 -04:00
|
|
|
var menuStyle = getComputedStyle($('.boardlist')[0]);
|
2016-05-09 05:18:27 -04:00
|
|
|
$((active_page == 'ukko') ? 'hr:first' : (active_page == 'catalog') ? 'body>span:first' : 'form[name="post"]').before(
|
2014-10-11 13:21:00 -04:00
|
|
|
$('<div id="watchlist">'+
|
|
|
|
'<div class="watchlist-controls">'+
|
2015-03-20 04:09:21 -04:00
|
|
|
'<span><a id="clearList">['+_('Clear List')+']</a></span> '+
|
|
|
|
'<span><a id="clearGhosts">['+_('Clear Ghosts')+']</a></span>'+
|
2014-10-11 13:21:00 -04:00
|
|
|
'</div>'+
|
|
|
|
threads.join('')+
|
2014-10-20 17:44:19 -04:00
|
|
|
'</div>').css("background-color", menuStyle.backgroundColor).css("border", menuStyle.borderBottomWidth+" "+menuStyle.borderBottomStyle+" "+menuStyle.borderBottomColor));
|
2014-10-11 13:21:00 -04:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [add /> adds the given item to the watchlist]
|
|
|
|
* @param {[Obj/Str]} sel [An unwrapped jquery selector.]
|
|
|
|
*/
|
|
|
|
watchlist.add = function(sel) {
|
|
|
|
var threadName, threadInfo;
|
|
|
|
|
2016-05-09 05:18:27 -04:00
|
|
|
var board_name = $(sel).parents('.thread').data('board');
|
|
|
|
|
2014-10-11 13:21:00 -04:00
|
|
|
if (active_page === 'thread') {
|
|
|
|
if ($('.subject').length){
|
|
|
|
//If a subject is given, use the first 20 characters as the thread name.
|
|
|
|
threadName = $('.subject').text().substring(0,20);
|
|
|
|
} else { //Otherwise use the thread id.
|
|
|
|
threadName = $('.op').parent().attr('id');
|
|
|
|
}
|
|
|
|
//board name, thread name as defined above, current amount of posts, thread url
|
|
|
|
threadInfo = [board_name, threadName, $('.post').length, location.href];
|
|
|
|
|
2016-05-09 05:18:27 -04:00
|
|
|
} else if (active_page === 'index' || active_page === 'ukko') {
|
2014-10-11 13:21:00 -04:00
|
|
|
|
|
|
|
var postCount;
|
|
|
|
//Figure out the post count.
|
|
|
|
if ($(sel).parents('.op').children('.omitted').length) {
|
|
|
|
postCount = $(sel).parents('.op').children('.omitted').text().split(' ')[0];
|
|
|
|
} else {
|
|
|
|
postCount = $(sel).parents('.op').siblings('.post').length+1;
|
|
|
|
}
|
2016-05-05 08:38:24 -04:00
|
|
|
//Grab the reply link.;
|
|
|
|
var threadLink = $(sel).siblings('a:not(.watchThread)').last().attr('href');
|
2014-10-11 13:21:00 -04:00
|
|
|
//Figure out the thread name. If anon, use the thread id.
|
|
|
|
if ($(sel).parent().find('.subject').length) {
|
|
|
|
threadName = $(sel).parent().find('.subject').text().substring(0,20);
|
|
|
|
} else {
|
|
|
|
threadName = $(sel).parents('div').last().attr('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
threadInfo = [board_name, threadName, postCount, threadLink];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
alert('Functionality not yet implemented for this type of page.');
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//if the thread is already being watched, cancel the function.
|
|
|
|
if (localStorage.watchlist.indexOf(JSON.stringify(threadInfo)) !== -1) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _watchlist = JSON.parse(localStorage.watchlist); //Read the watchlist
|
|
|
|
_watchlist.push(threadInfo); //Add the new watch item.
|
|
|
|
localStorage.watchlist = JSON.stringify(_watchlist); //Save the watchlist.
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [remove /> removes the given item from the watchlist]
|
|
|
|
* @param {[Int]} n [The index at which to remove.]
|
|
|
|
*/
|
|
|
|
watchlist.remove = function(n) {
|
|
|
|
var _watchlist = JSON.parse(localStorage.watchlist);
|
|
|
|
_watchlist.splice(n, 1);
|
|
|
|
localStorage.watchlist = JSON.stringify(_watchlist);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [clear /> resets the watchlist to the initial empty array]
|
|
|
|
*/
|
|
|
|
watchlist.clear = function() {
|
|
|
|
localStorage.watchlist = '[]';
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [exists /> pings every watched thread to check if it exists and removes it if not]
|
|
|
|
* @param {[Obj/Str]} sel [an unwrapped jq selector]
|
|
|
|
*/
|
|
|
|
watchlist.exists = function(sel) {
|
|
|
|
$.ajax($(sel).children().children('a').attr('href'), {
|
|
|
|
type :'HEAD',
|
|
|
|
error: function() {
|
|
|
|
watchlist.remove(parseInt($(sel).attr('id').split('-')[1])).render();
|
|
|
|
},
|
|
|
|
success : function(){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$(document).ready(function(){
|
2016-05-09 05:18:27 -04:00
|
|
|
if (!(active_page == 'thread' || active_page == 'index' || active_page == 'catalog' || active_page == 'ukko')) {
|
2014-11-08 19:58:02 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-11 13:21:00 -04:00
|
|
|
//Append the watchlist toggle button.
|
2017-05-31 18:36:30 -04:00
|
|
|
$('.boardlist .compact-boardlist').append('<span>[ <a class="watchlist-toggle" href="#">'+_('watchlist')+'</a> ]</span>');
|
2014-10-11 13:21:00 -04:00
|
|
|
//Append a watch thread button after every OP.
|
2015-03-20 04:09:21 -04:00
|
|
|
$('.op>.intro').append('<a class="watchThread" href="#">['+_('Watch Thread')+']</a>');
|
2014-10-11 13:21:00 -04:00
|
|
|
|
|
|
|
//Draw the watchlist, hidden.
|
|
|
|
watchlist.render();
|
|
|
|
|
|
|
|
//Show or hide the watchlist.
|
2014-11-22 02:34:03 -05:00
|
|
|
$('.watchlist-toggle').on('click', function(e) {
|
|
|
|
e.preventDefault();
|
2014-10-11 13:21:00 -04:00
|
|
|
//if ctrl+click, reset the watchlist.
|
|
|
|
if (e.ctrlKey) {
|
|
|
|
watchlist.render(true);
|
|
|
|
}
|
|
|
|
if ($('#watchlist').css('display') !== 'none') {
|
|
|
|
$('#watchlist').css('display', 'none');
|
|
|
|
} else {
|
|
|
|
$('#watchlist').css('display', 'block');
|
|
|
|
} //Shit got really weird with hide/show. Went with css manip. Probably faster anyway.
|
|
|
|
});
|
|
|
|
|
|
|
|
//Trigger the watchlist add function.
|
|
|
|
//The selector is passed as an argument in case the page is not a thread.
|
2014-11-22 02:34:03 -05:00
|
|
|
$('.watchThread').on('click', function(e) {
|
|
|
|
e.preventDefault();
|
2014-10-11 13:21:00 -04:00
|
|
|
watchlist.add(this).render();
|
|
|
|
});
|
|
|
|
|
|
|
|
//The index is saved in .watchlist-inner so that it can be passed as the argument here.
|
|
|
|
//$('.watchlist-remove').on('click') won't work in case of re-renders and
|
|
|
|
//the page will need refreshing. This works around that.
|
|
|
|
$(document).on('click', '.watchlist-remove', function() {
|
|
|
|
var item = parseInt($(this).parent().attr('id').split('-')[1]);
|
|
|
|
watchlist.remove(item).render();
|
|
|
|
});
|
|
|
|
|
|
|
|
//Empty the watchlist and redraw it.
|
|
|
|
$('#clearList').on('click', function(){
|
|
|
|
watchlist.clear().render();
|
|
|
|
});
|
|
|
|
|
|
|
|
//Get rid of every watched item that no longer directs to an existing page.
|
|
|
|
$('#clearGhosts').on('click', function() {
|
|
|
|
$('.watchlist-inner').each(function(){
|
|
|
|
watchlist.exists(this);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|