Prime Video Ad Blocker [ESP]

Skips ads and promos on Prime Video.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            Prime Video Ad Blocker [ESP]
// @name:es         Prime Video Ad Blocker [ESP]
// @namespace       https://greasyfork.org/en/users/5102-jeau
// @version         0.4.1
// @description     Skips ads and promos on Prime Video.
// @description:es  Bloquea los anuncios y promociones en Prime Video.
// @author          Jeau
// @license         MIT
// @match           https://*.amazon.com/*/video/*
// @match           https://*.amazon.co.uk/*/video/*
// @match           https://*.amazon.de/*/video/*
// @match           https://*.amazon.co.jp/*/video/*
// @match           https://*.primevideo.com/*
// @icon            https://m.media-amazon.com/images/G/01/digital/video/DVUI/favicons/favicon-32x32.png
// @grant           none
// ==/UserScript==

/*
-----------------------------------------------------------------------------------
  Adapted for Amazon Prime Video Spain. It might also work in other countries.
  Based on RawMeatEater's script:
  https://greasyfork.org/es/scripts/446723-amazon-video-ad-blocker
-----------------------------------------------------------------------------------
*/

(function() {
    'use strict';

    // Track if current ad block has been skipped
    let adSkipped = false;
    const adTimeRegExp = /(\d?\d:){0,2}\d?\d/;

    setInterval(() => {
        // Target the video element inside the latest injected surface container
        const lastSurface = Array.from(document.querySelectorAll(".atvwebplayersdk-video-surface")).pop();
        const video = lastSurface ? lastSurface.querySelector('video') : null;

        const adEl = document.querySelector(".atvwebplayersdk-ad-timer-remaining-time");

        // Execute skip logic if video is playing, ad timer is found, and ad hasn't been skipped yet
        if (video && video.currentTime && adEl && !adSkipped) {
            const match = adEl.textContent.match(adTimeRegExp);
            if (match) {
                // Parse time components directly into seconds using unary operator (+)
                const secs = match[0].split(':').reduce((accumulator, time) => (accumulator * 60) + (+time), 0);
                video.currentTime += secs;
                adSkipped = true;

                // DEBUG
                console.log([
                    '==========================',
                    'PRIME VIDEO AD SKIPPED !!!',
                    `Skipped: ${secs} seconds.`,
                    '=========================='
                ].join('\n'));
            }
        } else if (!adEl) {
            // Reset state only when the ad element completely disappears from the DOM
            adSkipped = false;
        }
    }, 200);

})();