Alt + Click on one or more images in a post to quote them.
当前为
// ==UserScript==
// @name Quote specific images in a post.
// @author Joshh
// @namespace https://tljoshh.com
// @version 0.1.1
// @description Alt + Click on one or more images in a post to quote them.
// @match *://*.websight.blue/thread/*
// @match *://*.websight.blue/threads/*
// @match *://websight.blue
// @match *://websight.blue/multi/*
// @license MIT
// ==/UserScript==
(function() {
'use strict';
let lastQuotedMsg = null;
const replyBox = document.querySelector('#reply-content');
const posts = document.querySelectorAll('.post');
// Listen to all posts for an alt+click event
for (const post of posts) {
post.addEventListener('click', handleClick);
};
// Target event: alt+click on images in a post
function handleClick(e) {
const { altKey, target, currentTarget } = e;
if (altKey && target.tagName === 'IMG') {
e.preventDefault();
handleImageClick(currentTarget, target);
}
};
// Add the markdown formatted text for the quoted message identifier and the image to the reply textarea
function handleImageClick(post, image) {
// Append quoted message identifier
const authorBoxNotAdded = !checkIfAuthorBoxAdded(post);
if(authorBoxNotAdded) {
addAuthorBox(post);
}
// Append quoted image to replybox
addQuotedImage(image);
}
// Add an image to the reply textarea
function addQuotedImage(img) {
const { alt, src } = img;
replyBox.value += `\n> `;
};
// Add quoted message identifier to reply textarea and store the permalink id
function addAuthorBox(post) {
const username = post.querySelector('.post-author').innerText;
const messageTopLinks = post.querySelectorAll('.message-top > a');
const filteredThreadPermalink = messageTopLinks[2].href;
const date = messageTopLinks[1].querySelector('.desktop-only').innerText;
const messagePermalink = messageTopLinks[1].href;
if(lastQuotedMsg !== null && replyBox.value.length) {
replyBox.value += `\n\n`;
} else if(replyBox.value.length) {
replyBox.value += `\n`;
}
replyBox.value += `> From: [${username}](${filteredThreadPermalink}) at [${date}](${messagePermalink})`;
lastQuotedMsg = messagePermalink;
};
// Check what the last quoted message identifier was and determine if we need to add an author box.
function checkIfAuthorBoxAdded(post) {
const messageTopLinks = post.querySelectorAll('.message-top > a');
const messagePermalink = messageTopLinks[1].href;
console.log('lastQuotedMsg', lastQuotedMsg);
console.log('messagePermalink', messagePermalink);
return lastQuotedMsg === messagePermalink
};
})();