while trying to firegure out how to embed bluesky posts i cam across a post by the author of the xenforo media plugin and he said he doesn't explicity support adult conetnt sites so i decided to create embed support for redgifs urls.
created with llama 3.1 sonar large 128k chat
edit:
just noticed the bookmarklets have a // comments in them so they won't work when you add it as a bookmark,
you'll have to manually remove those lines like // Construct iframe source URL by replacing 'watch' with 'ifr' before adding it to bookmarks but it'll run just fine in browser console.
created with llama 3.1 sonar large 128k chat
Bookmarklets and Userscripts Overview
- Purpose:
- These scripts are designed to replace links to RedGIFs videos on a webpage with embedded iframes, allowing you to watch the videos directly on the page.
- Functionality:
- Without Blur:
- When you click the bookmarklet or enable the userscript, it finds all links to RedGIFs videos on the page and replaces them with iframes. This lets you watch the videos without leaving the page.
- With Blur:
- In addition to embedding the videos, this version also blurs the iframes initially. You can click on the blurred area to unblur it and watch the video as normal.
- Without Blur:
Detailed Explanation
- Finding Links:
- The scripts look for links that point to RedGIFs videos.
- They check both regular links (<a> tags) and image links (<img> tags) with a data-url attribute.
- Replacing Links with Iframes:
- When a RedGIFs link is found, it is replaced with an iframe that shows the video.
- The iframe source URL is constructed by changing "watch" to "ifr" in the original link.
- Blur Functionality:
- For the blurred version, an overlay is added over the iframe.
- When you click on this overlay, it removes the blur effect, allowing you to watch the video.
- Userscripts vs Bookmarklets:
- Bookmarklets: These are small pieces of JavaScript code that you can save as bookmarks in your browser. When clicked, they run on the current webpage.
- Userscripts: These are scripts that run automatically on specific websites when you have a userscript manager like Tampermonkey or Greasemonkey installed.
In Simple Terms
- Without Blur:
- Clicking the bookmarklet or enabling the userscript will turn RedGIFs links into playable videos right on the page.
- With Blur:
- The same as above, but the videos will be blurred until you click on them to unblur and watch.
Bookmarklet Without Blur Functionality
Bookmarklet With Blur Functionality
Original Code to Final Code Changes
1. Lazy Loading of Iframes
Original: Iframes were loaded immediately upon replacing the links and images.
Final: Iframes are now loaded lazily using IntersectionObserver to load them only when they come into view.
2. Placeholder Elements
Original: Directly replaced elements with iframes.
Final: Replaced elements with placeholder divs, which are then replaced with iframes when they come into view.
3. Data Attribute for iframeSrc
Original: iframeSrc was constructed and used immediately.
Final: iframeSrc is stored in a data attribute of the wrapper div for later use when loading the iframe.
4. Preload Attribute
Original: No preload attribute was used.
Final: Added preload="metadata" to iframes to reduce initial load times by only loading metadata initially.
5. Pause/Play on Visibility
Original: No management of video playback based on visibility.
Final: Implemented an IntersectionObserver to pause/play videos based on their visibility to save CPU and memory resources.
6. Event Handling Refinement
Original: Click event listener was added to unblur iframes.
Final: Click event listener now also loads the iframe if it hasn't been loaded yet, ensuring that iframes are loaded either on click or when they come into view.
7. Error Handling and Null Checks
Original: No explicit checks for null values.
Final: Added checks to ensure that url, href, and data-url attributes are not null before using them.
Layman's Terms Summary
Lazy Loading: Instead of loading all videos at once, the new code loads them only when you scroll to where they are on the page. This saves a lot of memory and CPU power.
Placeholder Elements: Before loading a video, a placeholder is shown. This helps in managing resources better.
Efficient Data Storage: The video URL is stored in a hidden attribute of the placeholder until it's needed, making the code more efficient.
Preloading Metadata: When loading videos, only the metadata (like video length) is loaded first, reducing initial load times.
Pause/Play Videos: Videos are paused when you scroll away from them and resume playing when you scroll back. This saves resources by not playing videos that are not visible.
Improved Event Handling: Clicking on a blurred video now loads it if it hasn't been loaded yet, ensuring that videos are loaded either when you click on them or when they come into view.
Error Prevention: The new code checks for potential errors like missing URLs before trying to load videos, making it more robust.
These changes make the bookmarklet more efficient, reducing memory and CPU usage while still providing a smooth user experience.
1. Lazy Loading of Iframes
Original: Iframes were loaded immediately upon replacing the links and images.
Final: Iframes are now loaded lazily using IntersectionObserver to load them only when they come into view.
2. Placeholder Elements
Original: Directly replaced elements with iframes.
Final: Replaced elements with placeholder divs, which are then replaced with iframes when they come into view.
3. Data Attribute for iframeSrc
Original: iframeSrc was constructed and used immediately.
Final: iframeSrc is stored in a data attribute of the wrapper div for later use when loading the iframe.
4. Preload Attribute
Original: No preload attribute was used.
Final: Added preload="metadata" to iframes to reduce initial load times by only loading metadata initially.
5. Pause/Play on Visibility
Original: No management of video playback based on visibility.
Final: Implemented an IntersectionObserver to pause/play videos based on their visibility to save CPU and memory resources.
6. Event Handling Refinement
Original: Click event listener was added to unblur iframes.
Final: Click event listener now also loads the iframe if it hasn't been loaded yet, ensuring that iframes are loaded either on click or when they come into view.
7. Error Handling and Null Checks
Original: No explicit checks for null values.
Final: Added checks to ensure that url, href, and data-url attributes are not null before using them.
Layman's Terms Summary
Lazy Loading: Instead of loading all videos at once, the new code loads them only when you scroll to where they are on the page. This saves a lot of memory and CPU power.
Placeholder Elements: Before loading a video, a placeholder is shown. This helps in managing resources better.
Efficient Data Storage: The video URL is stored in a hidden attribute of the placeholder until it's needed, making the code more efficient.
Preloading Metadata: When loading videos, only the metadata (like video length) is loaded first, reducing initial load times.
Pause/Play Videos: Videos are paused when you scroll away from them and resume playing when you scroll back. This saves resources by not playing videos that are not visible.
Improved Event Handling: Clicking on a blurred video now loads it if it hasn't been loaded yet, ensuring that videos are loaded either when you click on them or when they come into view.
Error Prevention: The new code checks for potential errors like missing URLs before trying to load videos, making it more robust.
These changes make the bookmarklet more efficient, reducing memory and CPU usage while still providing a smooth user experience.
Userscripts
Userscript Without Blur Functionality
Userscript With Blur Functionality
Explanation
- Bookmarklets:
- The first bookmarklet replaces RedGIFs links with iframes without any blur functionality.
- The second bookmarklet includes blur functionality and an overlay to capture clicks.
- Userscripts:
- The first userscript replaces RedGIFs links with iframes without any blur functionality.
- The second userscript includes blur functionality and an overlay to capture clicks, similar to the bookmarklet.
edit:
just noticed the bookmarklets have a // comments in them so they won't work when you add it as a bookmark,
you'll have to manually remove those lines like // Construct iframe source URL by replacing 'watch' with 'ifr' before adding it to bookmarks but it'll run just fine in browser console.
Last edited: