Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.17k stars 416 forks source link

Not Working GM_download #631

Closed ManEXpen closed 3 years ago

ManEXpen commented 5 years ago

Expected Behavior

Should start downloading.

Actual Behavior

Does Not start if i give particular string as file name. ex)【合作】New Year's Dreams 2018 ~ YA☆RI☆MA☆SU☆NE[NYD 2018].mp4

Specifications

Script

the src: destination movie. name: do not start when i give it. onerror: called callback and my console showed undefined

GM_download({url: src,
                        name: `${title}[${author}].mp4`,
                        onerror: function(err){console.log(err.error)}});

Full Source code

// ==UserScript==
// @name         MyNdownloader
// @namespace    tests
// @version      0.2
// @description  はいいいえ
// @author       You
// @match          *://www.nicovideo.jp/*
// @match          *://ext.nicovideo.jp/
// @match          *://ext.nicovideo.jp/#*
// @match          *://blog.nicovideo.jp/*
// @match          *://ch.nicovideo.jp/*
// @match          *://com.nicovideo.jp/*
// @match          *://commons.nicovideo.jp/*
// @match          *://dic.nicovideo.jp/*
// @match          *://ex.nicovideo.jp/*
// @match          *://info.nicovideo.jp/*
// @match          *://search.nicovideo.jp/*
// @match          *://uad.nicovideo.jp/*
// @match          *://api.search.nicovideo.jp/*
// @match          *://*.nicovideo.jp/smile*
// @match          *://site.nicovideo.jp/*
// @exclude        *://ads.nicovideo.jp/*
// @exclude        *://www.upload.nicovideo.jp/*
// @exclude        *://www.nicovideo.jp/watch/*?edit=*
// @exclude        *://ch.nicovideo.jp/tool/*
// @exclude        *://flapi.nicovideo.jp/*
// @exclude        *://dic.nicovideo.jp/p/*
// @grant       GM.download
// @grant       GM_download
// @grant       GM.registerMenuCommand
// @grant       GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';

    let keyDownloadMov = 'r';
    let keyDownloadMylist = 'a';

    var isBusy = false;

    GM.registerMenuCommand(_('Download Movie'), saveMov, keyDownloadMov);

    GM.registerMenuCommand(_('Download Movie(mylist)'), async function () {
        var oldSrc = '';
        setInterval(function () {
            try {
                let src = document.querySelector('#ZenzaWatchVideoPlayerContainer > video').src;
                if (oldSrc !== src) {
                    oldSrc = src;
                    saveMov();
                }

            } catch (e) { clearInterval(); }
        }, 1000);
    }, keyDownloadMylist);

    async function saveMov() {
        console.log("Working has started");
        try {
            let src = document.querySelector('#ZenzaWatchVideoPlayerContainer > video').src;

            var author = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-dmcAvailable.is-dmcPlaying.showVideoControlBar.showVideoHeaderPanel.is-open > div.zenzaWatchVideoInfoPanel.show.userVideo > div.tabs.videoInfoTab.activeTab > div > div.zenzaWatchVideoInfoPanelContent > div.videoOwnerInfoContainer > span > span');
            if (!author) author = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-dmcAvailable.is-dmcPlaying > div.zenzaWatchVideoInfoPanel.show.userVideo > div.tabs.videoInfoTab.activeTab > div > div.zenzaWatchVideoInfoPanelContent > div.videoOwnerInfoContainer > span > span');
            if (!author) author = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-playing > div.zenzaWatchVideoInfoPanel.show.userVideo > div.tabs.videoInfoTab.activeTab > div > div.zenzaWatchVideoInfoPanelContent > div.videoOwnerInfoContainer > span > span');
            if (!author) author = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-dmcAvailable.is-dmcPlaying.is-pausing > div.zenzaWatchVideoInfoPanel.show.userVideo > div.tabs.videoInfoTab.activeTab > div > div.zenzaWatchVideoInfoPanelContent > div.videoOwnerInfoContainer > span > span');

            if (!author){
                var xpathStr1 = '//*[@id="zenzaVideoPlayerDialog"]/div/div[2]/div[8]/div[3]/div/div[1]/div[1]/span/span'
                author = document.evaluate(xpathStr1,document,null,9,null).singleNodeValue
            }

            author = author.innerText;
            author = author.slice(0, author.length - 3);

            var title = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-dmcAvailable.is-dmcPlaying.showVideoControlBar.showVideoHeaderPanel.is-open > div.zenzaWatchVideoHeaderPanel.show.userVideo > h2 > span');
            if (!title) title = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-dmcAvailable.is-dmcPlaying.is-playing > div.zenzaWatchVideoHeaderPanel.show.has-Parent.userVideo > h2 > span');
            if (!title) title = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-playing > div.zenzaWatchVideoHeaderPanel.show.has-Parent.userVideo > h2 > span');
            if (!title) title = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-dmcAvailable.is-dmcPlaying.is-playing > div.zenzaWatchVideoHeaderPanel.show.userVideo > h2 > span');
            if (!title) title = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-dmcAvailable.is-dmcPlaying.is-pausing > div.zenzaWatchVideoHeaderPanel.show.userVideo > h2 > span');
            if (!title) title = document.querySelector('#zenzaVideoPlayerDialog > div > div.zenzaPlayerContainer.is-showComment.is-regularUser.is-open.is-playing > div.zenzaWatchVideoHeaderPanel.show.userVideo > h2 > span');

            if (!title){
                var xpathStr = '//*[@id="zenzaVideoPlayerDialog"]/div/div[2]/div[9]/h2/span'
                title = document.evaluate(xpathStr,document,null,9,null).singleNodeValue
            }
            title = title.innerText;

            console.log("Download will Start");
            console.log(`src: ${src} ${title}[${author}].mp4`);
            GM_download({url: src,
                        name: `${title}[${author}].mp4`,
                        onerror: function(err){console.log(err.error)}});
        } catch (e) { alert('cant download'); throw new Error(e.name + ': ' + e.message); }
    }
derjanb commented 4 years ago

Fixed at TM BETA 4.11.6116.

Owyn commented 3 years ago

name: do not start when i give it.

I have the opposite problem, download errors "not_whitelisted" if I DON'T give name param

Stable TM + stable FireFox

BloodyRain2k commented 1 year ago

I'm currently struggling to get this command working too for a Pixiv image downloader and it's a total pain...

First of all am I required to whitelist everything as filename or it will NOT stop throwing "not_whitelisted" at me. I literally need to whitelist /.*/ or it will not stop. But even when I do that it instead shows "not_succeeded - SERVER FORBIDDEN", whatever that means.

What's irritating me the most about that error is that the browser doesn't even start a request in the first place...

image There's the error before I whitelist everything and after, same url. This is when the download mode is set to Browser API. If I set it to Native it says nothing more than "Forbidden". The whitelisting error is unaffected of the mode change.

I even tried setting the name parameter of the download object, but that didn't change anything either.

TM version 4.18.1 and Firefox 91.7.1esr (64-bit) on Win 7

Owyn commented 1 year ago

@BloodyRain2k pixiv checks for referrer when giving out images else it answers forbidden

BloodyRain2k commented 1 year ago

Yeah, after debugging the extension for an hour, because until I did that it's requests for whatever reason never showed up in the diagnostics, I figured out too that the headers were fed the wrong way...

I copied the download object from a different extension that used the internal version and the headers were formatted differently.

What I also don't understand at all is that I now no longer need the complete whitelisting... It just works fine now for whatever reason...

So my issue is no longer related to the extension, just basic http headers.

But for what's it worth would I still like to leave feedback on the lack of details on the error messages: I don't know if it's a "bug" of my older Firefox version that the requests of the extension don't show up in the console nor the networking tab, or if that's normal, but it would really be helpful when the error would print more info than only the status code text. I personally would prefer to instead get the xmlHttp object. It would've sped up the process of finding out that headers and cookies are missing.

As for the processing of the headers object, I'd say it's a little "too forgiving" with malformed data... Since I started with a wrong example it was given as

headers: [
    { name: "Referer", value: window.location.href },
],

and the current processing happily eats the array while an object is technically expected, and not a single "meep" is being thrown back. At least a warning would be nice. I was already wondering why, after finally finding the request results, there was a 0: [object Object] header.

Edit: well, one semi-extension related problem remains: I don't know how to get the stupid PHPSESSID cookie for the request... With all other requests it's a fight to prevent Firefox from sending ALL cookies, and here I have the opposite problem because this thing's restricted for security reasons.

derjanb commented 1 year ago

I have the opposite problem, download errors "not_whitelisted" if I DON'T give name param

name is required since 2014. Before a default name File.download was used.

Owyn commented 1 year ago

I have the opposite problem, download errors "not_whitelisted" if I DON'T give name param

name is required since 2014. Before a default name File.download was used.

That's a shame btw, since some files would lose their intended for download names

e.g. on deviant art if you use GM_Download you'll only get gibberish instead of filename: image

but if you simply press "save image as" and use the name servers gives you - you'd get a readable name with artist name and the title of the work: image

BloodyRain2k commented 1 year ago

name is required since 2014. Before a default name File.download was used.

That's another thing I found weird: why doesn't it default to the filename from the download url? It's not the hardest thing to just regex the end part of the url for that, which is exactly why I'm surprised that's not done by default.

Aphexus commented 1 month ago

I am getting similar errors:

Download Error: filename must not contain illegal characters for '2024-07-24T22꞉09꞉20.327Z (1⧸1) And just like that…ahoy 🏴‍☠️ - Kopheus.jpg'

The issue seems to be the pirate flag which seems to be certain unicode issues. Some unicode emojis do work fine.

E.g., doing name =name.replaceAll("🏴‍☠️", " "); works fine.

I run into this issue every once in a while with certain emoji's. I'd like to keep them or have some way to filter them out in the general case without overfiltering.