uk / 24ur.com brez oglasov

// ==UserScript==
// @name        24ur.com brez oglasov
// @namespace   24ur
// @description 24ur.com brez nadležnih oglasov in reklam, tudi med videi.
// @version     2016.7.18
// @include     http://24ur.com/*
// @include     http://www.24ur.com/*
// @exclude     htt*://*/adserver/*
// @run-at      document-end
// @require     https://cdnjs.cloudflare.com/ajax/libs/video.js/5.10.7/video.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/3.0.2/videojs-contrib-hls.js
// @grant       none
// ==/UserScript==

var hide_ads = true;

var css = '.video-js .vjs-big-play-button {left: 50%;top: 50%;margin-left: -0.6em;margin-top: -0.6em;width: 1.2em;height: 1.2em;line-height: 1.2em;' +
    'border-radius: 100%;font-size: 10em;border: 0;background:rgba(20, 20, 20, 0.7);box-shadow: 0 0 0 3px #fff inset;}' +
    '.video-js:hover .vjs-big-play-button, .video-js .vjs-big-play-button:focus {background: black;transition: all 0.4s;}' +
    '.video-js .vjs-control-bar {background:rgba(0, 0, 0, 0.7); font-size: 1.13333333em;margin: 0  1em 1em;border-radius: 0.4em;width: auto;}' +
    '.video-js.vjs-audio .vjs-control-bar {margin: 0;border-radius: 0;}' +
    '.vjs-audio .vjs-fullscreen-control {display:none;} .video-js.vjs-audio .vjs-control-bar { display: flex;}' +
    '.vjs-fullscreen .vjs-control-bar {font-size: 1.66666666em;}' +
    '.video-js .vjs-mute-control, .video-js .vjs-volume-menu-button, .video-js .vjs-fullscreen-control {width: 3.5em;}' +
    '.vjs-menu-button-popup.vjs-volume-menu-button-vertical .vjs-menu {bottom: .5em;height: 8em;width: 2.9em;left: .25em;}' +
    '.vjs-menu-button-popup.vjs-volume-menu-button-vertical .vjs-menu-content {height: auto;border-radius: 0.4em;background:rgba(0, 0, 0, 0.7);}' +
    '.video-js .vjs-current-time {display:block;}' +
    '.video-js .vjs-progress-control .vjs-mouse-display {background:none;}' +
    '.video-js .vjs-play-progress:after {display: none !important;}' +
    '.video-js .vjs-mouse-display:after {font-family: Arial, Helvetica, sans-serif;} .vjs-poster {background-color: black}';

if (hide_ads) {
  css += 'div[id*=banner], div[class*=banner], div[id*=Banner] {display: none !important} #article {display: block !important;}' +
    'body.banner_front {background: #FBFBFB url("/static/slo/main/img/backgrounds/body_bg.jpg") no-repeat center 23px !important;}' +
    'body.banner_front .side-padding {width: 980px !important; margin: auto !important;}' +
    'body.banner_front #head {margin: 0 !important; padding: 10px 0 !important;}';
}

document.head.innerHTML += '<link href="https://cdnjs.cloudflare.com/ajax/libs/video.js/5.10.7/video-js.min.css" rel="stylesheet"/><style type="text/css">' + css + '</style>';

// Checking for video or audio on page
if (window === parent) {
  var is_video = false;
  var is_audio = false;
  var count = 0;
  interval = setInterval(function () {
    ++count;
    if (count >= 20 || is_video && is_audio) clearInterval(interval);
    // Videoteka
    if (window.media_id != '' && window.article_id == '') {
      if (document.getElementById('mediacenter2') ||
          document.getElementById('flvvideo_free').getElementsByClassName('videoplayer_notice_renew') [0]) {
        var url = document.getElementsByClassName('article-link') [0].pathname;
        requestJSON(url, initizeVideo);
        clearInterval(interval);
      }
    }

    var video;
    if (!is_video && (video = document.getElementById('video_content'))) {
      if (count === 20 ||
          video.getElementById('mediacenter2') ||
          video.getElementById('flvvideo_free').getElementsByClassName('videoplayer_notice_renew') [0]) {
        is_video = true;
        requestJSON(null, initizeVideo);
      }
    }

    var audio;
    if (!is_audio && (audio = document.getElementById('audio_content'))) {
      if (count === 20 ||
          audio.getElementsByTagName('embed') [0] ||
          audio.getElementsByClassName('videoplayer_notice_renew') [0]) {
        is_audio = true;
        var parent = document.getElementById('audio_content').getElementsByTagName('div');
        var audio_n = 0;
        for (var i = 0; i < parent.length; i++) {
          if (parent[i].id.indexOf('flvaudio') > - 1) {
            var media_id = parent[i].id.match('[0-9]+');
            requestXML(media_id, audio_n + 'audio', loadAudio);
            audio_n++;
          }
        }
      }
    }
  }, 20);
}

var json;
var height = parseInt(window.player_height) + 40;
var width = window.player_width;

function requestXML(id, section, callback) {
      var req = new XMLHttpRequest();
      req.open("POST", 'http://www.24ur.com/bin/player/?mod=serve&site_id=1&media_id=' + id + '&section_id=' + section, true);
      req.onload = function(e) {
        if (req.status == 200) {
          if (section.indexOf('audio') > -1) {
            var xmlDoc = req.responseXML;
            var vid = xmlDoc.getElementsByTagName('item')[0];
            var type = vid.getAttribute("type");
            var url = 'http://vid01.24ur.com/' + vid.getAttribute('src') + '.mp3';
            var text = vid.getAttribute("txt");

            callback(parseInt(section), id, url, text);
          } else {
            callback(req.responseText);
          }
        } else {
          console.error(req.status);
        }
      };
      req.send();
}

function requestJSON(url, callback) {
  if (!url)
    url = 'http://api.24ur.si/spored/article/get' + location.pathname;
  else
    url = 'http://api.24ur.si/spored/article/get' + url;

  var xhr = new XMLHttpRequest();
  xhr.open("POST", url, true);
  xhr.setRequestHeader('Accept', 'application/json');
  xhr.onload = function(e) {
    if (xhr.status == 200) {
      callback(xhr.responseText, "json");
    } else {
      console.error(xhr.status);
    }
  };
  xhr.send();
}

function loadAudio(index, media_id, url, text) {
  var player_container = document.getElementById("flvaudio_" + media_id.toString());
  var new_player = 'new_html5_audio_' + media_id.toString();

  // add empty audio element
  player_container.innerHTML = '<audio id="' + new_player + '" class="video-js vjs-default-skin" controls preload="auto" data-setup="{}" />';
  player_container.innerHTML += '<div class="title" id="videojs_free_current_title" style="width:' + width +'px">' + text + '</div>';
  player_container.style.textAlign = "";

  videojs(new_player, {}, function() {
    this.height(40);
    this.width(width);
    this.bigPlayButton.hide();
    this.src(url);

    if (!this.el().hasAttribute('tabIndex'))
      this.el().setAttribute('tabIndex', '-1');

    this.one('play', function() {
      this.el().focus();
    });

    // v.js hotkeys based on https://github.com/ctd1500/videojs-hotkeys
    // hotkeys events
    this.on('keydown', vjs_hotkeys);

    this.on('error', function() {
      if (this.error().code === 3 || this.error().code === 4) {
        window.audio_player_setup(index);
      }
    });
  });
}

function initizeVideo(data, type) {
  json = JSON.parse(data);

  if (!document.getElementById("new_html5_video")) {
    setupVideoPlayer();
  }

  // replace all additional video links
  if (document.getElementById('more_video_content')) {
    var links = document.getElementById('more_video_content').getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
      links[i].setAttribute('onclick', 'next_video(' + links[i].getAttribute('onclick').match('[0-9]+') + ');return false;');
    }
  }

  getVideoInfo(window.media_id);
}

function getVideoInfo (data) {
  // if is media_id, use json parse
  if (data.toString().match(/^[0-9]+$/) != null && json != null) {
    for (var i = 0; i < Object.keys(json.article.videos).length; i++) {
      var video_id = json.article.videos[i].id;
      if (data == video_id) {
        var url = json.article.videos[i].url;
        url = url.replace('-1.mp4', '-2.mp4'); //use HQ video
        if (!url.match('vid01.24ur.com'))
          url = url.replace(url.match('vid[^.]+'), 'vid01');
        url = [{ type: "video/mp4", src: url }]

        var poster = json.article.videos[i].images[0].href.replace('PLACEHOLDER', '720x400');
        if (!poster)
          poster = '';

        return loadVideo(data, url, poster, "json");
      }
    }
    // Video not exist in json, use xml method
    return requestXML(data, window.section_id, getVideoInfo);
  } else { // use xml parser
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(data, "application/xml");
    var video = xmlDoc.getElementsByTagName('item')[0];
    var mimetype = video.getAttribute('mimetype');
    var media_id = video.getAttribute('id');
    var poster = 'http://image2.24ur.com/' + video.getAttribute('thumb').replace('*SIZE*', '720x400');
    if (mimetype == "mp4") {
      var url = 'http://vid01.24ur.com/' + video.getAttribute('src') + '-2.mp4';
      url = [{ type: "video/mp4", src: url }];

      return loadVideo(media_id, url, poster, "xml");
    }
    else if (mimetype == "m3u8") {
      var m3u8 = video.getAttribute("src").replace("##JWT##", "http://api.24ur.si/video");
      var mp4 = video.getAttribute("thumbs_dir").match("\\d{4}\/.+").toString().replace("_timethumb/","");
      mp4 = 'http://vid01.24ur.com/' + mp4 + '-2.mp4';
      url = [
        { type: "application/x-mpegURL", src: m3u8 },
        { type: "video/mp4", src: mp4 }
      ];

      return loadVideo(media_id, url, poster, "xml");
    } else {
      console.error("Nepodprt format: " + mimetype);
    }
  }
}

function loadVideo(id, url, poster, type) {
  if (!document.getElementById("new_html5_video")) {
    setupVideoPlayer();
  }

  relatedVideos(window.section_id, window.article_id, id);

  // add video source and poster
  var switched_source = false;
  var video_player = videojs('new_html5_video');
  video_player.src(url);
  video_player.poster(poster);

  // if html5 video not work, fallback to flash video
  video_player.on('error', function(e) {
    if (this.error().code != 1) {
      if(typeof(url) == "object" && url.length > 1 && !switched_source) {
        video_player.src(url[1]);
        switched_source = true;
        return;
      }

      window.video_change(id, '0', 0);
    }
  });
}

function setupVideoPlayer() {
  var video_frame = document.getElementById('flvvideo_free');
  video_frame.parentNode.style.display = 'block';

  // add empty video element
  var html = '<video id="new_html5_video" class="video-js vjs-default-skin" controls preload="auto" data-setup="{}" />';
  video_frame.innerHTML = html;

  // setup video.js player
  videojs('new_html5_video', {
    controlBar: {
      children: {
        'playToggle': {},
        'currentTimeDisplay': {},
        'timeDivider': {},
        'durationDisplay': {},
        'progressControl': {},
        'remainingTimeDisplay': {},
        'volumeMenuButton': {
          vertical: true,
          inline: false
        },
        'fullscreenToggle': {}
      }
    }
  }, function() {
    this.height(height);
    this.width(width);

    // insert block for related videos
    var related = document.createElement('div');
    related.id = 'vjs_related';
    related.style.display = 'none';
    this.el().appendChild(related);

    if (!this.el().hasAttribute('tabIndex'))
      this.el().setAttribute('tabIndex', '-1');

    this.one('play', function() {
      this.el().focus();
    });

    // v.js hotkeys based on https://github.com/ctd1500/videojs-hotkeys
    // hotkeys events
    this.on('keydown', vjs_hotkeys);

    // Double click - Toggle Fullscreen
    this.on('dblclick', function(e) {
      e.preventDefault();
      // Double click on video, ignore clicks on control bar
      if ((e.toElement || e.target) == this.el().querySelector('.vjs-tech')) {
        if (this.isFullscreen())
          this.exitFullscreen();
        else
          this.requestFullscreen();
      }
    });

    // show related videos when video ended
    this.on("ended", function() {
      this.handleTechLoadStart_();
      this.bigPlayButton.hide();
      this.exitFullscreen();
      related.style.display = '';

      this.on('playing', function() {
        related.style.display = 'none';
      });
    });
  });
}

window.relatedVideos = function(section_id, article_id, media_id) {
  var url = 'http://www.24ur.com/lbin/videojs_last_published.php?site_id=1&section_id=' + section_id + '&article_id=' + article_id + '&media_id=' + media_id;

  var req = new XMLHttpRequest();
  req.open("GET", url, true);
  req.onload = function(e) {
    if (req.status == 200) {
      document.getElementById('vjs_related').innerHTML = req.responseText.replace('javascript:videojs_replay()', 'javascript:next_video(\'replay\')');
      document.getElementById('videojs_current_image').style.height = height + 'px';
    }
  }
  req.send();
}

window.next_video = function(id) {
  var video = videojs('new_html5_video');
  if (id == 'replay')
    return video.play();

  getVideoInfo(id);
}

// video.js hotkeys based on https://github.com/ctd1500/videojs-hotkeys
var vjs_hotkeys = function(e) {
  var key = e.keyCode || e.which;

  // Prevent hotkeys when related videos are shown or fullscreen in audio player
  if (document.getElementById('vjs_related') && document.getElementById('vjs_related').style.display != 'none' ||
      this.el().hasClass('vjs-audio') && key === 70)
    return;

  // Space key - Toggle Play/Pause
  if (key === 32) {
    e.preventDefault();
    if (this.paused())
      this.play();
    else
      this.pause();
  }

  // M key - Toggle Mute
  if (key === 77) {
    e.preventDefault();
    this.muted(!this.muted());
  }

  // F key - Toggle Fullscreen
  if (key === 70) {
    e.preventDefault();
    if (this.isFullscreen())
      this.exitFullscreen();
    else
      this.requestFullscreen();
  }

  // Left key - Seek Backward - step 3 sec
  if (key === 37) {
    e.preventDefault();
    this.currentTime(this.currentTime() - 3)
  }

  // Right key - Seek Forward - step 3 sec
  if (key === 39) {
    e.preventDefault();
    this.currentTime(this.currentTime() + 3)
  }

  // Up key - Volume up - step 5%
  if (key === 38) {
    e.preventDefault();
    this.volume(this.volume() + 0.05);
  }

  // Up key - Volume down - step 5%
  if (key === 40) {
    e.preventDefault();
    this.volume(this.volume() - 0.05);
  }
}