Tumblr get video source

Gets the video source url for the first video on a tumblr post.

  1. // ==UserScript==
  2. // @name Tumblr get video source
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.6
  5. // @description Gets the video source url for the first video on a tumblr post.
  6. // @author You
  7. // @match https://*.tumblr.com/post/*
  8. // @match https://www.tumblr.com/video/*
  9. // @grant GM_setClipboard
  10. // @grant GM_registerMenuCommand
  11. // @run-at document-idle
  12. // ==/UserScript==
  13.  
  14. if(window.top !== window.self) {
  15. const videoSourceElem = document.querySelector('video source');
  16. if(!videoSourceElem){
  17. return;
  18. }
  19. window.top.postMessage({
  20. iframeVideoUrl: window.location.href,
  21. iframeVideoSrc: videoSourceElem.src
  22. }, "*")
  23. return
  24. }
  25.  
  26.  
  27. const parentPostID = window.location.href.split('/')[4]
  28. let iframeVideoSrc = ''
  29.  
  30. window.addEventListener('message', function(event) {
  31. if(!event.data.iframeVideoUrl || !event.data.iframeVideoSrc) {
  32. return
  33. }
  34. const iframeVideoID = event.data.iframeVideoUrl.split('/')[5]
  35.  
  36. if(parentPostID === iframeVideoID){
  37. iframeVideoSrc = event.data.iframeVideoSrc
  38. if(iframeVideoSrc.endsWith('/480')){
  39. iframeVideoSrc = iframeVideoSrc.slice(0, -3)
  40. }
  41. console.log(iframeVideoSrc)
  42. }
  43. }, false)
  44.  
  45. function getVideoUrl(){
  46. // GM_setClipboard(iframeVideoSrc)
  47. var textarea = document.createElement('textarea')
  48.  
  49. textarea.setAttribute('id', 'linksTextarea')
  50. textarea.setAttribute('style', `
  51. position: absolute;
  52. top: 60px;
  53. left:40px;
  54. width:1000px;
  55. height:500px;
  56. background-color:white;
  57. z-index:100000;
  58. padding:10px;
  59. `)
  60. textarea.value = iframeVideoSrc
  61. document.body.appendChild(textarea)
  62. window.scrollTo(0,0)
  63. }
  64.  
  65. GM_registerMenuCommand('Get Video Url', getVideoUrl)
  66.