The provided JavaScript code is a bookmarklet designed to copy Reddit post content to BBCode format, which is commonly used in forums like XenForo. Here's a detailed summary of what it does and how it works on both new.reddit.com and old.reddit.com:
Functionality Overview
- BBCode Conversion: The script converts HTML content from Reddit posts into BBCode format. This includes converting links, images, bold, italic, and strikethrough text into their respective BBCode equivalents.
- Code Block Handling: It handles code blocks by converting them into BBCode
[code]
or [icode]
tags based on the content's complexity.
- Image Link Handling: The script identifies links that end with image file extensions and handles them appropriately.
- Copy Button Creation: For each post or comment, it creates a copy button. When clicked, this button copies the post content in BBCode format, along with a permalink to the comment, to the clipboard.
- Spoiler Tags: The copied content is wrapped in spoiler tags to conceal it until clicked.
Detailed Explanation
BBCode Conversion
- Links: Converts HTML links (<a href="link">text</a>) to BBCode format (
[u][url]link[/url][/u]
).
- Images: Converts HTML images (<img src="image_url">) to BBCode format (
[img]image_url[/img]
).
- Bold, Italic, Strikethrough: Converts HTML bold (<b>text</b>), italic (<i>text</i>), and strikethrough (<s>text</s>) to BBCode (text,
[i]text[/i]
, [s]text[/s]
).
Code Block Handling
- Identifies code blocks within the HTML content.
- If a code block is short or contains no newline characters, it is converted to
content
; otherwise, it is converted to [code]content[/code]
.
Image Link Handling
- Checks links for image file extensions (e.g., .jpg, .png, .jpeg, .webp, .gif, .pnj).
- If a link ends with these extensions, it is handled as an image link.
Copy Button Creation
- Creates a green copy button (
) next to each post or comment.
- When clicked, it extracts the post content, converts it to BBCode, and copies it to the clipboard along with the permalink.
Spoiler Tags
- Wraps the copied content in spoiler tags (
[spoiler="text"]content[/spoiler]
) to hide it until clicked.
Compatibility with New and Old Reddit
The script should work on both new.reddit.com and old.reddit.com, as it targets generic HTML elements that are present in both versions. However, the exact functionality might vary slightly depending on differences in HTML structure between the two versions.
- New Reddit: The script should work seamlessly, as new Reddit uses modern HTML structures that the script is designed to handle.
- Old Reddit: The script might require slight adjustments if old Reddit uses significantly different HTML structures for posts and comments. However, the core functionality should remain intact.
Overall, this bookmarklet simplifies the process of copying Reddit content into a format suitable for forums that use BBCode, making it easier to share content across platforms.
edit:
update fix image links inside of img bbcode tags.