Tampermonkey: Is it possible to set local path for GM_download?

Created on 5 Sep 2017  路  1Comment  路  Source: Tampermonkey/tampermonkey

I have submitted the same question to StackOverflow, but I don't know if I should have posted it here.

I have written a Tampermonkey script to add a custom download option to a Flickr photo page. It works when download option in settings is set to native, but the file is always stored in my default downloads folder. Is there any way to set the local path of a download without user interaction?

// ==UserScript==
// @name         Flickr download to folder
// @namespace    http://malver.dk/
// @version      0.1
// @description  Download k-size photo from Flickr where available
// @author       Rasmus Malver
// @match        https://www.flickr.com/photos/*
// @grant        GM_download
// @grant        GM_info
// ==/UserScript==

(function() {
      'use strict';
      var scripts=document.getElementsByTagName("script");
      var rx = /modelExport:([^\n\r]+),/;
      var match;
      for(var i = 0;i < scripts.length; i++){
          if(scripts[i].className=="modelExport"){
              match = rx.exec(scripts[i].innerHTML);
              break;
          }
      }
      var json=JSON.parse(match[1]);
      var kUrl='http:'+json['photo-models'][0]['sizes']['k'].url;
      var d = new Date();
      var localName = "~/Documents/Flickr/"+d.toISOString().substr(0,10)+"-"+kUrl.substr(-28,11)+".jpg";
      // E.g. "~/Documents/Flickr/2017-09-05-36203459914.jpg"
      var arg = { url: kUrl,
              name: localName
            };
      var result= GM_download(arg);
      console.log(result);
})();

Most helpful comment

(Un)Fortunately you can (only) download files to a location relative to the download folder. 馃槈

Please note, that you have to enable browser API downloads by following these steps:

  • Set Config mode to Advanced
  • Search Download Mode and set it to Browser API
  • Press OK at the tab that opens after the step above
  • Prefix the the download file name with a folder

Example script:

// ==UserScript==
// @name         Flickr download to folder
// @namespace    http://malver.dk/
// @version      0.1
// @description  Download k-size photo from Flickr where available
// @author       Rasmus Malver
// @match        https://www.flickr.com/photos/*
// @grant        GM_download
// @grant        GM_info
// ==/UserScript==

(function() {
    'use strict';
    var scripts=document.getElementsByTagName("script");
    var rx = /modelExport:([^\n\r]+),/;
    var match;
    for(var i = 0;i < scripts.length; i++){
        if(scripts[i].className=="modelExport"){
            match = rx.exec(scripts[i].innerHTML);
            break;
        }
    }
    var json=JSON.parse(match[1]);
    var kUrl='http:'+json['photo-models'][0]['sizes']['k'].url;
    var d = new Date();
    var localName = d.toISOString().substr(0,10)+"-"+kUrl.substr(-28,11)+".jpg";

    var path = 'flickr/'; // use a special folder for all the images
    var arg = {
        url: kUrl,
        name: path + localName
    };
    debugger;
    var result= GM_download(arg);
    console.log(result);
})();

>All comments

(Un)Fortunately you can (only) download files to a location relative to the download folder. 馃槈

Please note, that you have to enable browser API downloads by following these steps:

  • Set Config mode to Advanced
  • Search Download Mode and set it to Browser API
  • Press OK at the tab that opens after the step above
  • Prefix the the download file name with a folder

Example script:

// ==UserScript==
// @name         Flickr download to folder
// @namespace    http://malver.dk/
// @version      0.1
// @description  Download k-size photo from Flickr where available
// @author       Rasmus Malver
// @match        https://www.flickr.com/photos/*
// @grant        GM_download
// @grant        GM_info
// ==/UserScript==

(function() {
    'use strict';
    var scripts=document.getElementsByTagName("script");
    var rx = /modelExport:([^\n\r]+),/;
    var match;
    for(var i = 0;i < scripts.length; i++){
        if(scripts[i].className=="modelExport"){
            match = rx.exec(scripts[i].innerHTML);
            break;
        }
    }
    var json=JSON.parse(match[1]);
    var kUrl='http:'+json['photo-models'][0]['sizes']['k'].url;
    var d = new Date();
    var localName = d.toISOString().substr(0,10)+"-"+kUrl.substr(-28,11)+".jpg";

    var path = 'flickr/'; // use a special folder for all the images
    var arg = {
        url: kUrl,
        name: path + localName
    };
    debugger;
    var result= GM_download(arg);
    console.log(result);
})();

Was this page helpful?
0 / 5 - 0 ratings