This will auto refresh on new posts. Also cleans things up a bit so that we don't send requests to the sys server that it isn't going to handle anyway.
Use it if you want.
(function($) {
"use strict";
/**
-
Text Utils
*/
class TextUtil {};
TextUtil.IMG_SELECTOR = 'img';
TextUtil.LINK_SELECTOR = 'a';
TextUtil.NEW_POST_EVENT = 'new_post';
TextUtil.POST_REPLY_SELECTOR = 'div.post.reply';
TextUtil.POST_FILE_SELECTOR = 'div.file';
TextUtil.MEDIA_SERVER = 'media.8kun.top'
TextUtil.SYS_SERVER = 'sys.8kun.top'
/**
-
Wrapper for 8kun active_page variable to determine the type of
-
page the user is on.
*/
class ActivePage {
/**
* Are we on a thread page?
* @return {boolean} True if on thread
*/
static isThread() {
return window.active_page == ActivePage.Thread;
}
}
ActivePage.Thread = 'thread';
class FixImages {
/**
* Construct an object to fix images
*/
constructor(addPostEvents = []) {
this._setupListeners();
this._fixExistingPosts();
}
/**
* Sets up listeners to run on certain events
*
* Runs fixPost on new post events
*/
_setupListeners() {
$(document).on(TextUtil.NEW_POST_EVENT, function(e, post) {
this._fixPost(post);
}.bind(this));
}
_fixPost(post) {
let pFiles = post.querySelectorAll(TextUtil.POST_FILE_SELECTOR);
pFiles.forEach(f ={
let links = f.querySelectorAll(TextUtil.LINK_SELECTOR);
links.forEach(l ={
// Don't replace unless the sys server can handle the request
let needsReplaced = l.href.match(/media.8kun.top\/.*[a-f0-9]{64}.(png|jpg|jpeg)/);
if (!needsReplaced) { return; }
let href = l.href.replace(TextUtil.MEDIA_SERVER, TextUtil.SYS_SERVER);
let pngMatch = href.match(/.*([a-f0-9]{64}).png$/);
if (pngMatch) {
let hash = pngMatch[1];
href = href.replace(${hash}.png
, ${hash}.png/${hash}.jpg
);
}
l.href = href;
let img = l.querySelector(TextUtil.IMG_SELECTOR);
if (!img) { return; }
img.src = l.href;
});
});
}
/**
* Fixes any posts that already exist
*/
_fixExistingPosts() {
let posts = Array.from(document.querySelectorAll(TextUtil.POST_REPLY_SELECTOR));
posts.forEach(p ={
this._fixPost(p);
});
}
}
/**
-
MAIN
*/
if (ActivePage.isThread()) { // Only setup if we are on a thread
$(document).ready(function() {
window.temp = {};
window.temp.fixImages = new FixImages();
});
}
}(window.jQuery));