Guest User

webm-title.user

a guest
Jan 23rd, 2016
1,861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        WebM title
  3. // @namespace   https://2chk.hk/webm-title
  4. // @description Show metadata title of WebM videos on 2ch.hk
  5. // @include     https://2ch.hk/*
  6. // @version     0.0.1
  7. // @grant       none
  8. // ==/UserScript==
  9.  
  10. // Ported from 4chan-x (MIT).
  11. function parseTitle(data) {
  12.   function readInt() {
  13.     var n = data[i++];
  14.     var len = 0;
  15.     while (n < (0x80 >> len)) {
  16.       len++;
  17.     }
  18.     n ^= (0x80 >> len);
  19.     while (len-- && i < data.length) {
  20.       n = (n << 8) ^ data[i++];
  21.     }
  22.     return n;
  23.   }
  24.  
  25.   var i = 0;
  26.   while (i < data.length) {
  27.     var element = readInt();
  28.     var size = readInt();
  29.     if (element === 0x3BA9) {  // Title
  30.       var title = "";
  31.       while (size-- && i < data.length) {
  32.         title += String.fromCharCode(data[i++]);
  33.       }
  34.       return decodeURIComponent(escape(title));  // UTF-8 decoding
  35.     } else if (element !== 0x8538067 && element !== 0x549A966) {  // Segment, Info
  36.       i += size;
  37.     }
  38.   }
  39.   return null;
  40. }
  41.  
  42. function fetchData(url) {
  43.   return new Promise(function(resolve, reject) {
  44.     var xhr = new XMLHttpRequest();
  45.     xhr.open("GET", url, true);
  46.     xhr.setRequestHeader("Range", "bytes=0-9999");
  47.     xhr.responseType = "arraybuffer";
  48.     xhr.onload = function() {
  49.       if (this.status >= 200 && this.status < 400) {
  50.         resolve(new Uint8Array(this.response));
  51.       } else {
  52.         reject(new Error(this.responseText));
  53.       }
  54.     };
  55.     xhr.onerror = reject;
  56.     xhr.send();
  57.   });
  58. }
  59.  
  60. document.addEventListener("DOMContentLoaded", function() {
  61.   var container = document.getElementById("fullscreen-container");
  62.   if (!container) return;
  63.   var observer = new MutationObserver(function(mutations) {
  64.     mutations.forEach(function(mutation) {
  65.       Array.prototype.filter.call(mutation.addedNodes, function(node) {
  66.         return node.tagName === "VIDEO";
  67.       }).forEach(function(video) {
  68.         var url = video.querySelector("source").src;
  69.         fetchData(url).then(function(data) {
  70.           var title = parseTitle(data);
  71.           if (!title) return;
  72.           var div = document.createElement("div");
  73.           div.textContent = title;
  74.           div.style.textAlign = "center";
  75.           div.style.fontStyle = "italic";
  76.           div.style.padding = "3px";
  77.           video.parentNode.appendChild(div);
  78.         })
  79.       });
  80.     });
  81.   });
  82.   observer.observe(container, {childList: true});
  83. }, false);
Add Comment
Please, Sign In to add comment