RES twitter expandos

Formats embedded tweets. RES cannot do this for technical and security reasons.

  1. // ==UserScript==
  2. // @name RES twitter expandos
  3. // @namespace com.example
  4. // @description Formats embedded tweets. RES cannot do this for technical and security reasons.
  5. // @match https://*.reddit.com/*
  6. // @version 1
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. "use strict";
  11.  
  12. /**
  13. * loadTwitterScript(): void
  14. * Adds the twitter script that was stripped by RES to the given node.
  15. */
  16. function loadTwitterScript(node) {
  17. node.style.display = 'none';
  18. let script = document.createElement('script');
  19. script.src = 'https://platform.twitter.com/widgets.js';
  20. node.appendChild(script);
  21. }
  22.  
  23. /**
  24. * observeForTwits(target: HTMLElement): void
  25. * Attaches a MutationObserver to target and loads twitter's js whenever it finds a tweet.
  26. */
  27. function observeForTwits(target) {
  28. let observer = new MutationObserver(function(mutations) {
  29. for (let mutation of mutations) {
  30. for (let node of mutation.addedNodes) {
  31. if (node.className == 'twitter-tweet') {
  32. loadTwitterScript(node);
  33. }
  34. }
  35. }
  36. });
  37.  
  38. // configuration of the observer:
  39. let config = { childList: true, subtree: true };
  40.  
  41. // pass in the target node, as well as the observer options
  42. observer.observe(target, config);
  43. }
  44.  
  45. observeForTwits(document.body);