Greasy Fork

Greasy Fork is available in English.

FB资料库视频下载插件

应对2024年2月1日fb资料库视频和无法右键下载的问题。 -脚本随缘更新

目前为 2024-02-01 提交的版本,查看 最新版本

// ==UserScript==
// @name         FB资料库视频下载插件
// @namespace    http://tampermonkey.net/
// @version      0.25
// @description  应对2024年2月1日fb资料库视频和无法右键下载的问题。 -脚本随缘更新
// @author       LYL
// @icon         https://www.google.com/s2/favicons?sz=64&domain=facebook.com
// @include     *://www.facebook.com/ads/library/*
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';


     timer = setInterval(function() {

         console.log("视频下载启动");

        var videoElements = document.querySelectorAll('.x1lliihq.x5yr21d.xh8yej3');

        // 遍历每个视频标签
        videoElements.forEach(function(videoElement) {
            // 检查是否已经添加了下载按钮
            if (videoElement.parentElement.querySelector('.download-button')) {
                return; // 如果已经存在按钮,则不重复添加
            }

            // 创建下载按钮
            var downloadButton = document.createElement('button');
            downloadButton.innerText = '下载';
            downloadButton.className = 'download-button';
            downloadButton.style.position = 'absolute';
            downloadButton.style.top = '0';
            downloadButton.style.right = '0';
            downloadButton.style.zIndex = '999';

            // 获取视频链接
            var videoSrc = videoElement.getAttribute('src');

            // 检查 videoSrc 是否为 null
            if (videoSrc === null) {
                console.error('Video source is null. Skipping download button creation.');
                return;
            }

            // 提取文件名部分
            var fileNameMatch = videoSrc.match(/\/([^\/?#]+)(?:[?#]|$)/);
            var fileName = fileNameMatch !== null ? fileNameMatch[1] : 'video.mp4';

            // 添加点击事件,点击时触发下载
            downloadButton.addEventListener('click', function() {
                // 创建一个隐藏的链接并设置下载属性
                var xhr = new XMLHttpRequest();
                xhr.open("GET", videoSrc, true);
                xhr.responseType = "blob";

                xhr.onload = function () {
                    var blob = new Blob([xhr.response], { type: "video/mp4" });

                    // 创建一个隐藏的链接并设置下载属性
                    var downloadLink = document.createElement('a');
                    downloadLink.href = window.URL.createObjectURL(blob);
                    downloadLink.download = fileName;

                    // 添加链接到页面并触发点击
                    document.body.appendChild(downloadLink);
                    downloadLink.click();

                    // 移除链接
                    document.body.removeChild(downloadLink);
                };

                xhr.send();
            });

            // 将按钮添加到视频元素上
            videoElement.parentElement.appendChild(downloadButton);
        });



    }, 3000)

})();