Bulk Export Dropbox Image URLs (2023)

Extracts image URLs from a Dropbox page and copies them to the clipboard when a button is clicked.

目前為 2023-04-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bulk Export Dropbox Image URLs (2023)
// @version      3.0
// @description  Extracts image URLs from a Dropbox page and copies them to the clipboard when a button is clicked.
// @author       Tyler Hall Tech <[email protected]>
// @supportURL   https://github.com/tyhallcsu/dropbox-image-url-extractor/issues/new
// @namespace    https://github.com/tyhallcsu/dropbox-image-url-extractor
// @homepageURL  https://github.com/tyhallcsu/dropbox-image-url-extractor
// @license      MIT
// @connect      greasyfork.org
// @connect      sleazyfork.org
// @connect      github.com
// @connect      openuserjs.org
// @match        https://www.dropbox.com/*
// @grant        GM_setClipboard
// @grant        GM_log
// @compatible   chrome
// @compatible   firefox
// @compatible   edge
// @compatible   opera
// @compatible   safari
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    const SECONDS_TO_WAIT_FOR_SCROLL = 1; // adjust as needed
    const DOWNLOAD_URL_REPLACEMENT = '?raw=1';

    // function to get all image link elements
    function getImageLinks() {
        const imageLinks = document.querySelectorAll('a.dig-Link.sl-link--file[href*="dl=0"]');
        return Array.from(imageLinks).map(link => link.getAttribute('href').replace(/\?dl=0$/, DOWNLOAD_URL_REPLACEMENT));
    }

    // function to scroll to the bottom of the page and wait for new images to load
    async function waitForImagesToLoad() {
        window.scrollTo(0, document.body.scrollHeight);
        await new Promise(resolve => setTimeout(resolve, SECONDS_TO_WAIT_FOR_SCROLL * 1000));
    }

    // create an array to hold the image URLs
    let imageUrls = [];

    // add a button to the page that will copy the image URLs to the clipboard when clicked
    const copyButton = document.createElement('button');
    copyButton.classList.add('dig-Button', 'dig-Button--primary', 'dig-Button--standard', 'copy-urls-button');
    copyButton.textContent = 'Copy all URLs';
    copyButton.style.position = 'fixed';
    copyButton.style.bottom = '20px';
    copyButton.style.right = '20px';
    copyButton.style.zIndex = '9999';
    document.body.appendChild(copyButton);

    // add a click event listener to the button
    copyButton.addEventListener('click', async function() {
        let finished = false;
        let numUrls = 0;
        while (!finished) {
            // scroll to the bottom of the page and wait for new images to load
            await waitForImagesToLoad();

            // get the newly loaded image URLs
            const newImageUrls = getImageLinks().filter(url => !imageUrls.includes(url));
            imageUrls.push(...newImageUrls);

            // check if all images have been loaded
            finished = newImageUrls.length === 0;

            numUrls += newImageUrls.length;
        }

        // join the image URLs into a string separated by newlines
        const imageUrlString = imageUrls.join('\n');

        // copy the image URL string to the clipboard
        GM_setClipboard(imageUrlString, 'text');

        // disable the button and change the text to indicate that the URLs have been copied
        copyButton.disabled = true;
        copyButton.textContent = `${numUrls} URL(s) copied to clipboard`;

        // enable the button again after 3 seconds
        setTimeout(function() {
            imageUrls = [];
            copyButton.disabled = false;
            copyButton.textContent = 'Copy all URLs';
        }, 3000);
    });
})();