bnew

Veteran
Joined
Nov 1, 2015
Messages
57,901
Reputation
8,572
Daps
161,426
so fix formatting of html code


Here's a change log in layman's terms from the initial code to the updated code, with the updated code appended: Version 1: Initial Code
  • The initial code was designed to format text within a textarea element on a webpage.
  • It replaced URLs with a specific format, added newlines before certain characters, and removed unnecessary tags.
  • It also removed duplicate newlines and improved the formatting of headings and images.
Version 2: Updated Code
  • **Fixed issue with characters∗∗:Theupdatedcodereplaced‘characters**:Theupdatedcodereplaced`characters∗∗:Theupdatedcodereplaced‘ characters with the correct brackets (‘and‘`and`‘and‘`) to fix a formatting issue.
  • Improved newline handling: The code now adds newlines before specific characters, such as headings and images, to improve readability.
  • Added support for math notation: The code now replaces text enclosed in double dollar signs ($$...$$) with LaTeX-style math notation ($$$1$$).
  • Improved paragraph formatting: The code now adds newlines between paragraphs to improve readability.
  • Added support for quote tags: The code now wraps text enclosed in
    tags in and tags to improve formatting.
  • Removed unnecessary patterns: The code removed some unnecessary patterns, such as pattern8, to simplify the formatting process.
  • Improved code organization: The code is now more organized, with each pattern and replacement clearly defined and separated.
Changes to Patterns
  • pattern1: Updated to match URLs with the correct brackets ($$ and $$).
  • pattern2: Updated to add newlines before specific characters, such as headings and images.
  • pattern3: Updated to add newlines before images with alt text.
  • pattern4: Updated to replace newlines with two newlines.
  • pattern5: Updated to add newlines before headings, lists, and images.
  • pattern6 and pattern7: Removed, as they are no longer necessary.
  • mathPattern: Added to replace text enclosed in double dollar signs with LaTeX-style math notation.
  • paragraphPattern: Added to add newlines between paragraphs.
  • quotePattern: Added to wrap text enclosed in
    tags in and tags.
Code:
javascript
javascript:(function() {
  var textareas = document.querySelectorAll('textarea.input');
  textareas.forEach(function(textarea) {
    var text = textarea.value;
    var pattern1 = /$$URL=(.*?)$$$$\/URL$$/g;
    var pattern2 = /(\n)([A-Z"]|\d|$$B$$|$$I$$|$$IMG$$)/g;
    var pattern3 = /$$IMG alt="([^"]*)"/g;
    var pattern4 = /\n/g;
    var pattern5 = /($$HEADING=2$$|$$LIST$$|$$\*$$|$$\/LIST$$|$$IMG$$)/g;
    var pattern6 = /$$\/?FONT(=.*?)?$$/g;
    var pattern7 = /$$\/?COLOR(=.*?)?$$/g;
    var pattern8 = /\$\$\/IMG$$([A-Za-z0-9])/g;
    var pattern9 = /\n{2,}/g;

    var mathPattern = /\$\$(.*?)\$\$/g;

    var paragraphPattern = /([^\n])\n([A-Z0-9])/g;
   
    var replacedText = text.replace(pattern1, '[U][URL=$1[/URL][/U]');
    replacedText = replacedText.replace(pattern2, '$1\n$2');
    replacedText = replacedText.replace(pattern3, '\n[IMG alt="$1"');
    replacedText = replacedText.replace(pattern4, '\n\n');
    replacedText = replacedText.replace(pattern5, '\n$1');
    replacedText = replacedText.replace(pattern6, '');
    replacedText = replacedText.replace(pattern7, '');
    replacedText = replacedText.replace(pattern8, ']\n[/IMG]$1');
    replacedText = replacedText.replace(pattern9, '\n');
    replacedText = replacedText.replace(/$$U$$\$\$U\$\$URL=(.*?)\/URL\$\$\/U\$\$\/U$$/g, '[U][URL=$1[/URL][/U]');
    replacedText = replacedText.replace(/$$\/HEADING$$\n/g, '[/HEADING]');
    replacedText = replacedText.replace(/$$\/IMG$$\n/g, '[/IMG]');

    replacedText = replacedText.replace(mathPattern, '\$$$1\$$');

    replacedText = replacedText.replace(paragraphPattern, '$1\n\n$2');
   
    var quotePattern = /$$QUOTE$$(?:.*?\n)*?(.*?)$$\/QUOTE$$/gs;
    replacedText = replacedText.replace(quotePattern, '[QUOTE][SIZE=4]$1[/SIZE][/QUOTE]');

    textarea.value = replacedText;
  });
})();
 

bnew

Veteran
Joined
Nov 1, 2015
Messages
57,901
Reputation
8,572
Daps
161,426
To ensure that the script does not replace the same URL tags multiple times (if they've already been wrapped with [U]), we can modify the replacement logic to only apply the [U] wrapping if it hasn’t been applied already. One way to do this is by adding a condition to check if the [U] tags are already present.


Here’s an updated version of the code to address that:

Code:
javascript:(function() {
  var textareas = document.querySelectorAll('textarea.input');
  textareas.forEach(function(textarea) {
    var text = textarea.value;
    
    
    var pattern1 = /\[URL\](.*?)\[\/URL\]/g; 
    var pattern2 = /\[URL=(.*?)\](.*?)\[\/URL\]/g; 
    
    
    var pattern3 = /(\n)([A-Z"]|\d|\[B\]|\[I\]|\[IMG\])/g;
    var pattern4 = /\[IMG alt="([^"]*)"/g;
    var pattern5 = /\n/g;
    var pattern6 = /(\[HEADING=2\]|\[LIST\]|\[\*\]|\[\/LIST\]|\[IMG\])/g;
    var pattern7 = /\[\/?FONT(=.*?)?\]/g;
    var pattern8 = /\[\/?COLOR(=.*?)?\]/g;
    var pattern9 = /\$\$\/IMG\]([A-Za-z0-9])/g;
    var pattern10 = /\n{2,}/g;

    var mathPattern = /\$\$(.*?)\$\$/g;

    var paragraphPattern = /([^\n])\n([A-Z0-9])/g;

    
    var replacedText = text.replace(pattern1, function(match, url) {
      
      if (!/\[U\]/.test(match)) {
        return `[U][URL]${url}[/URL][/U]`;
      }
      return match;
    });
    
    replacedText = replacedText.replace(pattern2, function(match, url, innerText) {
      
      if (!/\[U\]/.test(match)) {
        return `[U][URL=${url}]${innerText}[/URL][/U]`;
      }
      return match;
    });

    replacedText = replacedText.replace(pattern3, '$1\n$2');
    replacedText = replacedText.replace(pattern4, '\n[IMG alt="$1"');
    replacedText = replacedText.replace(pattern5, '\n\n');
    replacedText = replacedText.replace(pattern6, '');
    replacedText = replacedText.replace(pattern7, '');
    replacedText = replacedText.replace(pattern8, ']\n[/IMG]$1');
    replacedText = replacedText.replace(pattern10, '\n');
    replacedText = replacedText.replace(/\[U\]\$\$U\$\$URL=(.*?)\/URL\$\$\/U\$\$\/U\]/g, '[U][URL=$1[/URL][/U]');
    replacedText = replacedText.replace(/\[\/HEADING\]\n/g, '[/HEADING]');
    replacedText = replacedText.replace(/\[\/IMG\]\n/g, '[/IMG]');

    replacedText = replacedText.replace(mathPattern, '\\[$1\\]');

    replacedText = replacedText.replace(paragraphPattern, '$1\n\n$2');
    
    var quotePattern = /\[QUOTE\](?:.*?\n)*?(.*?)\[\/QUOTE\]/gs;
    replacedText = replacedText.replace(quotePattern, '[QUOTE][SIZE=4]$1[/SIZE][/QUOTE]');

    textarea.value = replacedText;
  });
})();


Key Changes:​


  1. Pattern Matching for [U][/B]: In the replacement functions for both pattern1 (the [URL]...[/URL] pattern) and pattern2 (the [URL=]...[/URL] pattern), I’ve added a check to see if the text is already wrapped in [U] using the regular expression /\[U\]/. If the text is already wrapped in [U], it will skip the wrapping and leave the original match as is.
    • This avoids wrapping [URL] tags multiple times.
    [*]Replacement Functions: I used replace() with a callback function, which checks each match and ensures that [U] tags are only added if not already present.



Example Input:

Code:
🛰️ Paper: [URL]https://arxiv.org/abs/2412.10360[/URL]
🌌 Website: [URL]https://apollo-lmms.github.io[/URL]



Expected Output:

Code:
🛰️ Paper: [U][URL]https://arxiv.org/abs/2412.10360[/URL][/U]
🌌 Website: [U][URL]https://apollo-lmms.github.io[/URL][/U]



Example After Running the Script Twice:


If you run the script on the same text twice, it will not apply the [U] tags again, ensuring that the result remains unchanged after the first execution.
 

bnew

Veteran
Joined
Nov 1, 2015
Messages
57,901
Reputation
8,572
Daps
161,426
updated the bluesky post extract bookmarklet to get playlist urls for videos moree reliably since the old one only retrieved one playlist url no matter how many vidos were in a thread.

*NOTE: quoted posts not working.. :(

What Does This Code Do?​

This code is a bookmarklet designed to work on Bluesky (BSky) posts and threads. When you run it on a Bluesky thread or profile page, it performs several tasks to extract and format the content of the posts in a user-friendly way. Here's a detailed breakdown of its functionality:

1. Prompts You to Specify How Many Posts to Extract​

- When you click the bookmarklet, it shows a pop-up window with a calculator-like interface.
- You can enter the number of posts you want to extract (e.g., 5, 10, or 20).
- If you enter an invalid number (like 0 or a negative number), it will ask you to try again.

2. Extracts Posts from the Thread​

- After you specify the number of posts, the script scans the Bluesky thread or profile page.
- It identifies all the posts made by the original author of the thread (ignoring replies from other users unless specified otherwise).
- It collects the following details for each post:
  • Author's username (prefixed with a 🇺 emoji for visual distinction).
  • Post content (the text of the post).
  • Embedded links (e.g., playlist links, video links, or image links).
  • Quoted posts (if the post references or quotes another post, it includes the quoted content).
  • Images (if the post contains images, it adds them as clickable links).

3. Fetches Additional Data from Bluesky's API​

- For each post, the script makes a call to Bluesky's public API to fetch additional details, such as:
  • Embedded media (e.g., playlists, videos, or images).
  • Post metadata (e.g., unique post IDs and URLs).
- If the post contains a playlist, video, or image, the script appends the link to the post's text.

4. Formats the Extracted Content​

- The script organizes the extracted posts into a structured and readable format:
  • Each post is numbered (e.g., "1/5", "2/5") to indicate its position in the thread.
  • The author's username is displayed with the 🇺 emoji (e.g., "🇺 username").
  • Links, quoted posts, and images are clearly marked and formatted for easy reading.
  • The entire thread is wrapped in spoiler tags (e.g., [SPOILER="thread continued"]) to make it compact and easy to navigate.

5. Copies the Formatted Text to Your Clipboard​

- Once the script finishes extracting and formatting the posts, it automatically copies the entire formatted text to your clipboard.
- You can then paste the text anywhere (e.g., a forum, document, or chat) without needing to manually copy and format each post.

6. Shows a Notification​

- After copying the text, the script displays a small notification at the bottom-left corner of the screen to let you know how many posts were copied (e.g., "Copied: 5 posts").
- The notification fades away after a few seconds.

7. Handles Errors Gracefully​

- If something goes wrong (e.g., the script can't find posts or fetch data from the API), it will log the error in the browser's console and continue processing the remaining posts.
- This ensures that the script doesn't break completely if it encounters an issue.

What Does the Final Output Look Like?​

Here’s an example of what the formatted output might look like when you paste it:

Code:
https://bsky.app/profile/username/post/12345

[SPOILER="thread continued"]
https://bsky.app/profile/username/post/12346
https://bsky.app/profile/username/post/12347
[/SPOILER]

[SPOILER="full text & large images"]

1/3
🇺 username
This is the first post in the thread.
[U][URL]https://example.com/playlist[/URL][/U]

2/3
🇺 username
This is the second post.
[Quoted post]
🇺 another_user
https://bsky.app/profile/another_user/post/67890
This is the quoted post content.

3/3
🇺 username
This is the third post with an image.
[img]https://example.com/image.jpg[/img]

[COLOR=rgb(184,49,47)][B][SIZE=5]To post tweets in this format, more info here: [URL]https://www.thecoli.com/threads/tips-and-tricks-for-posting-the-coli-megathread.984734/post-52211196[/URL][/SIZE][/B][/COLOR]
[/SPOILER]

Why Is This Useful?​

- Saves Time: Instead of manually copying and formatting each post, this script automates the process.
- Preserves Context: It includes all relevant details (e.g., links, quoted posts, images) in a structured format.
- Easy Sharing: The formatted text is ready to be pasted into forums, blogs, or other platforms that support BBCode or similar formatting.
- Customizable: You can choose how many posts to extract, making it flexible for both short and long threads.

Who Would Use This?​

- Forum Users: People who want to share Bluesky threads on forums that use BBCode formatting.
- Content Creators: Bloggers or writers who want to archive or reference Bluesky posts in their work.
- Casual Users: Anyone who wants to save or share Bluesky threads in a clean, readable format.

Conclusion​

In summary, this script is a powerful tool for extracting, formatting, and sharing Bluesky posts and threads with minimal effort. It’s designed to make your life easier when dealing with long or complex threads! Let me know if you have further questions or need additional features. 😊



v3.6

DeepSeek-V3 fixed the issue :in one shot :wow:

protip: when posting to the coli an output of more than 6,000 characters switch to bbcode editor so the post goes through without any 500 interval error message appearing.
 
Last edited:
Top