save link as anchor name

Ask for help about FlashGot, no registration needed to post
Guest

save link as anchor name

Post by Guest » Thu Feb 04, 2016 9:34 pm

Is there a way to download what a link points to and name the downloaded file with the anchor name in the link? An example where this would be useful is a page which is a table of contents and the names on the links are sensible, but the file names in the uris are not.
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0

User avatar
therube
Ambassador
Posts: 7406
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: save link as anchor name

Post by therube » Fri Feb 05, 2016 3:42 pm

I'm thinking that may depend on the particular download manager you use.
Some may pick up "pretty" names, others can only garner the name of the URL.
(My download manager does not do "pretty".)

(I was wondering if there were any way to supply [any] name using browser built-in? As far as I can see, it only allows you to designate a directory for the files to go into.)
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 5.1; rv:42.0) Gecko/20100101 SeaMonkey/2.39

Guest

Re: save link as anchor name

Post by Guest » Sat Feb 06, 2016 1:46 pm

You're right, it's certainly possible for the download manager to decide how to name the resulting files. I'll have to try looking for one that does it how I like.

I don't think that there is any way to use firefox built-in download manager out of the box to skip the prompt and name the files in this way. I've been using a custom bit of code to do it:

Code: Select all

function SaveNamer_onSaveNamedLink()
{
 // use selected text as file name if there is any
 var fileName = document.commandDispatcher.focusedWindow.getSelection().toString();

 // use link to name the file otherwise
 if (!fileName) fileName = gContextMenu.linkText();

 // remove misbehaving characters
 fileName = fileName.replace(/^\s+|\/|\?|\:|\%|\&|\'|\"|\;|\!|\s+$/g, "");

 //https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIFile
 //https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O
 var dest = Components.classes["@mozilla.org/file/directory_service;1"]
  .getService(Components.interfaces.nsIProperties)
  .get("Desk", Components.interfaces.nsIFile);
 dest.append(fileName + ".html");

  var aChosenData = new AutoChosen(dest,
  makeURI(gContextMenu.linkURL, gContextMenu.target.ownerDocument.characterSet));
 var aReferrer = makeURI(gContextMenu.target.ownerDocument.URL,
  gContextMenu.target.ownerDocument.characterSet);

 //toolkit/content/contentAreaUtils.js
 internalSave(/*aURL*/ null, /*aDocument*/ null, /*aDefaultFileName*/ null,
  /*aContentDisposition*/ null, /*aContentType*/ null,
  /*aShouldBypassCache*/ true, /*aFilePickerTitleKey*/ null,
  //aChosenData, aReferrer, true); return; dummy( // Iceweasel 17
  aChosenData, aReferrer, /*aInitiatingDocument*/ document,
  /*aSkipPrompt*/ true, /*aCacheKey*/ null);
}

The problem with this code is that the new firefox extension signing disables it. I'd like to be able to use flashgot instead.
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0

User avatar
therube
Ambassador
Posts: 7406
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: save link as anchor name

Post by therube » Sat Feb 06, 2016 7:53 pm

A couple "name" Prefs, but don't know if they'll be of benefit?

> flashgot.media.forceNameHack
> flashgot.media.guessName
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0 SeaMonkey/2.39

Guest

Re: save link as anchor name

Post by Guest » Sun Feb 21, 2016 3:31 pm

Actually, as far as I can tell, there's no way to pass the link text to the download manager. In the documentation https://flashgot.net/features#customdm, there's no option for passing the link text at all. So, if I write a script wrapper, I can't name the downloaded file according to this information. And I don't believe any other download manager can either. Also, I experimented by changing flashgot.media.forceNameHack and flashgot.media.guessName. And, I wasn't able to see any effect from doing that.

At this point, I don't think it is possible for the download manager to name files according to the link text.
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0

Guest

Re: save link as anchor name

Post by Guest » Tue Feb 23, 2016 8:19 am

Taking the path of least resistance, I submitted my naming extension to firefox. There's now a signed version available: https://addons.mozilla.org/en-US/firefox/addon/savenamer/
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0

User avatar
Thrawn
Senior Member
Posts: 3106
Joined: Mon Jan 16, 2012 3:46 am
Location: Australia
Contact:

Re: save link as anchor name

Post by Thrawn » Tue Feb 23, 2016 11:08 pm

Kudos for proactivity! However, since you used XUL, your extension is already deprecated.

Want to test whether it works on other browsers (eg Seamonkey, Pale Moon) where it might have more future?
======
Thrawn
------------
Religion is not the opium of the masses. Daily life is the opium of the masses.

True religion, which dares to acknowledge death and challenge the way we live, is an attempt to wake up.
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0

User avatar
therube
Ambassador
Posts: 7406
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: save link as anchor name

Post by therube » Wed Feb 24, 2016 3:16 pm

I don't understand how this is to work?
What download manager are we using, or does it matter?
What type of links, or does that matter?
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 5.1; rv:42.0) Gecko/20100101 SeaMonkey/2.39

Post Reply