Bunkrr video show thumbnail

Include thumbnail in bunkrr video view page

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Bunkrr video show thumbnail
// @namespace    Violentmonkey Scripts
// @version      0.3
// @description  Include thumbnail in bunkrr video view page
// @author       walmart22
// @match        https://*.bunkrr.su/*
// @license      MIT
// @grant        none
// ==/UserScript==

// Run immediately if ready otherwise queue up
if (document.readyState !== 'loading') {
  insertThumbnail();
} else {
  document.addEventListener('DOMContentLoaded', insertThumbnail);
}

function insertThumbnail() {
  const videoExtension = ['mp4', 'm4v', 'mov', 'mkv'];
  const domain = 'bunkrr'
  const thumbDomain = 'bunkr'
  const url = window.location.href;

  let modifiedUrl = url;
  if (!videoExtension.some(ext => url.toLowerCase().includes(`.${ext}`))) {
    // If the url doesn't include the extension, build the url using the video src
    let video_url = document.querySelector('source').src.split('/').pop();
    modifiedUrl = url.split('/').slice(0, -1).join('/') + '/' + video_url;
  }

  let downloadLink = Array.from(document.querySelectorAll("a")).find(el => el.textContent.trim().startsWith('Download'));
  let prefix = new URL(downloadLink.href).host.split('.')[0];
  let thumbnailBase = modifiedUrl.replace(new RegExp(`\.(${videoExtension.join('|')})`, 'ig'), '.png')
  // Two options seen in the wild
  let thumbnail1 = thumbnailBase.replace(`//${domain}.su/v/`, `//i-${prefix}.${thumbDomain}.ru/thumbs/`);
  let thumbnail2 = thumbnailBase.replace(`//${domain}.su/v/`, `//${prefix}img.${thumbDomain}.ru/thumbs/`);

  if (thumbnail1.indexOf('i-media-files') !== -1) {
    thumbnail2 = thumbnail1.replace("i-media-files", "i");
  }
  // If the first thumbnail fails to load, try the second one
  let onError = `this.onerror=null;this.src='${thumbnail2}';src="${thumbnail2}"`
  let template = document.createElement('template');
  template.innerHTML = `<img src="${thumbnail1}" alt="img" loading="lazy" style="display: inline" onError="this.onerror=null;this.src='${thumbnail2}'">`

  let place = document.querySelector('#statsLink');
  if (!place) {
    place = document.querySelector('body > main > section:nth-child(2) > div > div > div > div:nth-child(2) > div > a');
  }
  place.parentElement.prepend(template.content);
}