Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name External Video Player for Android Chromium
- // @description 1 tap - Player 1, 2 taps - Player 2, Long press - Reset. Support for VLC, MX, JustPlayer, mpvEx, Custom.
- // @version 2
- // @author Kamik
- // @match *://*/*
- // @grant none
- // @run-at document-idle
- // ==/UserScript==
- (function () {
- "use strict";
- // Constants for storage and IDs
- const KEY_SINGLE = "extVideo_p1";
- const KEY_DOUBLE = "extVideo_p2";
- const BTN_ID = "ext-video-main-btn";
- const PLAYERS = [
- { name: "VLC", pkg: "org.videolan.vlc" },
- { name: "MX Player Free", pkg: "com.mxtech.videoplayer.ad" },
- { name: "MX Player Pro", pkg: "com.mxtech.videoplayer.pro" },
- { name: "Just Player", pkg: "com.brouken.player" },
- { name: "mpvEx", pkg: "app.marlboroadvance.mpvex" },
- { name: "Other…", pkg: "__other__" },
- ];
- let lastVideoUrl = null;
- let hideTimer = null;
- let tapCount = 0;
- let tapTimer = null;
- const vibrate = (ms) => navigator.vibrate && navigator.vibrate(ms);
- /* ===== Core Video Detection ===== */
- function findVideoUrl() {
- const v = document.querySelector("video");
- if (v && v.currentSrc && !v.currentSrc.startsWith("blob:"))
- return v.currentSrc;
- const s = document.querySelector("source[src]");
- if (s && s.src) return s.src;
- try {
- const html = document.documentElement.innerHTML;
- const m = html.match(
- /https?:\/\/[^"'\\\s]+?\.(mp4|m3u8|webm|mkv)(\?[^"'\\\s]*)?/i
- );
- return m ? m[0] : null;
- } catch {
- return null;
- }
- }
- /* ===== Cross-Frame Communication (Top Window Logic) ===== */
- if (window.self !== window.top) {
- // Limited check in frames to save battery
- let frameCheckCount = 0;
- const frameInterval = setInterval(() => {
- const url = findVideoUrl();
- if (url) {
- window.top.postMessage({ type: "VIDEO_FOUND", url: url }, "*");
- clearInterval(frameInterval); // Stop interval once video found
- }
- if (++frameCheckCount > 20) clearInterval(frameInterval); // Stop after 40s
- }, 2000);
- return;
- }
- /* ===== Cleanup & Lifecycle ===== */
- function removeButton() {
- const btn = document.getElementById(BTN_ID);
- if (btn) btn.remove();
- lastVideoUrl = null;
- }
- // Check on navigation (Back/Forward)
- window.addEventListener("popstate", () => {
- setTimeout(() => {
- if (!findVideoUrl()) removeButton();
- }, 500);
- });
- window.addEventListener("message", (e) => {
- if (e.data && e.data.type === "VIDEO_FOUND") {
- lastVideoUrl = e.data.url;
- createButton();
- }
- });
- /* ===== UI: Player Selection Menu ===== */
- function showMenu(storageKey) {
- vibrate(30);
- return new Promise((resolve) => {
- const overlay = document.createElement("div");
- Object.assign(overlay.style, {
- position: "fixed",
- inset: 0,
- background: "rgba(0,0,0,0.8)",
- zIndex: 2147483647,
- display: "flex",
- alignItems: "center",
- justifyContent: "center",
- backdropFilter: "blur(4px)",
- });
- const box = document.createElement("div");
- Object.assign(box.style, {
- background: "#121212",
- borderRadius: "20px",
- padding: "15px",
- width: "80%",
- maxWidth: "300px",
- border: "2px solid #2ecc71",
- color: "#fff",
- });
- const title = document.createElement("div");
- title.textContent =
- storageKey === KEY_SINGLE ? "Slot 1: Single Tap" : "Slot 2: Double Tap";
- title.style.cssText =
- "text-align:center; padding-bottom:12px; color:#2ecc71; font-weight:bold; border-bottom:1px solid #333; margin-bottom:10px;";
- box.appendChild(title);
- PLAYERS.forEach((p) => {
- const row = document.createElement("div");
- row.textContent = p.name;
- row.style.cssText =
- "padding:14px; text-align:center; border-radius:10px; margin:4px 0; background:#1e1e1e; font-family:sans-serif;";
- row.onclick = (e) => {
- e.stopPropagation();
- let pkg = p.pkg;
- if (pkg === "__other__") {
- pkg = prompt("Enter Package Name:");
- if (!pkg) {
- overlay.remove();
- resolve(null);
- return;
- }
- }
- localStorage.setItem(storageKey, pkg);
- overlay.remove();
- resolve(pkg);
- };
- box.appendChild(row);
- });
- overlay.onclick = () => {
- overlay.remove();
- resolve(null);
- };
- overlay.appendChild(box);
- document.body.appendChild(overlay);
- });
- }
- /* ===== UI: Main Button (Fixed Viewport) ===== */
- function createButton() {
- if (document.getElementById(BTN_ID)) return;
- const btn = document.createElement("div");
- btn.id = BTN_ID;
- Object.assign(btn.style, {
- position: "fixed",
- left: "15px",
- bottom: "30px",
- width: "48px",
- height: "48px",
- borderRadius: "50%",
- background: "rgba(0, 0, 0, 0.9)",
- border: "2.5px solid #2ecc71",
- display: "flex",
- alignItems: "center",
- justifyContent: "center",
- zIndex: 2147483646,
- transition: "opacity 0.4s ease, transform 0.3s ease",
- });
- btn.innerHTML = `<div style="width:0; height:0; border-left:16px solid #2ecc71; border-top:10px solid transparent; border-bottom:10px solid transparent; margin-left:4px;"></div>`;
- let pressTimer;
- btn.addEventListener(
- "touchstart",
- (e) => {
- e.preventDefault();
- pressTimer = setTimeout(() => {
- vibrate(200);
- localStorage.removeItem(KEY_SINGLE);
- localStorage.removeItem(KEY_DOUBLE);
- alert("All player assignments cleared.");
- tapCount = 0;
- }, 700);
- },
- { passive: false }
- );
- btn.addEventListener("touchend", () => {
- clearTimeout(pressTimer);
- tapCount++;
- if (tapCount === 1) {
- tapTimer = setTimeout(async () => {
- const url = findVideoUrl() || lastVideoUrl;
- if (tapCount === 1) {
- vibrate(35);
- let pkg = localStorage.getItem(KEY_SINGLE);
- if (!pkg) pkg = await showMenu(KEY_SINGLE);
- if (pkg) openIntent(url, pkg);
- } else {
- vibrate([35, 50, 35]);
- let pkg = localStorage.getItem(KEY_DOUBLE);
- if (!pkg) pkg = await showMenu(KEY_DOUBLE);
- if (pkg) openIntent(url, pkg);
- }
- tapCount = 0;
- }, 300);
- }
- });
- document.body.appendChild(btn);
- }
- /* ===== Smooth Scrolling Logic ===== */
- window.addEventListener(
- "scroll",
- () => {
- const btn = document.getElementById(BTN_ID);
- if (!btn) return;
- btn.style.opacity = "0";
- btn.style.transform = "scale(0.7)";
- clearTimeout(hideTimer);
- hideTimer = setTimeout(() => {
- btn.style.opacity = "1";
- btn.style.transform = "scale(1)";
- }, 600);
- },
- { passive: true }
- );
- /* ===== Observer Engine (High Performance) ===== */
- const observer = new MutationObserver(() => {
- const url = findVideoUrl();
- if (url) {
- lastVideoUrl = url;
- createButton();
- }
- });
- observer.observe(document.documentElement, {
- childList: true,
- subtree: true,
- });
- function openIntent(url, pkg) {
- if (!url) return;
- const intent = `intent:${url}#Intent;package=${pkg};type=video/*;end;`;
- window.location.href = intent;
- }
- })();
Advertisement