Vyhľadajte odpoveď

Vyhnite sa podvodom s podporou. Nikdy vás nebudeme žiadať, aby ste zavolali alebo poslali SMS na telefónne číslo alebo zdieľali osobné informácie. Nahláste prosím podozrivú aktivitu použitím voľby “Nahlásiť zneužitie”.

Learn More

How to save automaticaly the URL as shortcut file for download?

  • 7 odpovedí
  • 1 má tento problém
  • 445 zobrazení
  • Posledná odpoveď od master233742

more options

Hello there,

It's probably a simple one line of code solution, but I don't find the answer or how to do it.

I started yesterday writing add-on code for Firefox using javascript. I'll try to explain what I want to accomplish here.

There is a feature with the browser where we can drag and drop the website URL to the desktop to create a shortcut file that will open the website at the URL address. This works fine, but it can be tiring manual work.

I'm trying to accomplish the same result, but with code so that when I download a file with the add-on its URL shortcut is also automatically saved.

I searched enough to see at least one other add-on that seem to be able to save a URL as file, but there's a few problem. One is that the file does not contain the thumbnail like the original URL created from the browser itself. Another is that the file size takes 10 to 100 times more storage space. Lastly, when I open it Windows display a warning message that needs to be answered to continue. That extra step gives a bad experience. I admit that I haven't looked at the code yet, but I suspect that it won't do the result that I want even if I understand how it works which I'll take a chance to do.

Overall, I would say that I found nothing like the default drag and drop option.

I'm afraid that if I ask if is there such feature the answer will be negative.

Any help is welcome. Thanks.

Hello there, It's probably a simple one line of code solution, but I don't find the answer or how to do it. I started yesterday writing add-on code for Firefox using javascript. I'll try to explain what I want to accomplish here. There is a feature with the browser where we can drag and drop the website URL to the desktop to create a shortcut file that will open the website at the URL address. This works fine, but it can be tiring manual work. I'm trying to accomplish the same result, but with code so that when I download a file with the add-on its URL shortcut is also automatically saved. I searched enough to see at least one other add-on that seem to be able to save a URL as file, but there's a few problem. One is that the file does not contain the thumbnail like the original URL created from the browser itself. Another is that the file size takes 10 to 100 times more storage space. Lastly, when I open it Windows display a warning message that needs to be answered to continue. That extra step gives a bad experience. I admit that I haven't looked at the code yet, but I suspect that it won't do the result that I want even if I understand how it works which I'll take a chance to do. Overall, I would say that I found nothing like the default drag and drop option. I'm afraid that if I ask if is there such feature the answer will be negative. Any help is welcome. Thanks.

Vybrané riešenie

I simply wanted to do a follow up and say that I found the solution to all of the concerns.

I'm using a background script with all the appropriate permission.

I ran into some trouble getting access to the data and creating the file.

Creating the file was a problem because there were unsupported characters by Windows in the file's name.

For some reason I was getting a bing url when the name was not allowed and that was very confusing.

I solved by I implementing a for loop to parse the string of the file name and replace the chars causing troubles. I was able to reproduce the same name as the drag and drop features (with the exception that Instead of loosing some chars for a space I put a '-' instead.

Here's the code sample I wrote :

  //title gives a bing search using the words of the real title.
  //Seems like it is related to the fact that the title's string contains unsupported chars by windows OS for file name.
  var tabname = currentTab.title;

  //To be a valide file name, some forbidden chars must be removed from the title :
  //# % & { } \ < > * ? / $ ! ' " : @ + ` | =
  //replace it with '-' to keep some of the layout's structure in the name

tabname = tabname.trimLeft();

//In JavaScript, strings are immutable.
//which means the best you can do is to create a new string with
//the changed content and assign the variable to point to it.

//There is no replaceAt function in JavaScript.

var tempStringArray = tabname.split("");
var tempStringArrayLength = tempStringArray.length;
for (var i = 0; i < tempStringArrayLength; i++) {
switch (tempStringArray[i]) {
  case ':':
    tempStringArray[i] = '-';
    break;
  case '|':
    tempStringArray[i] = '-';
    break;
  case '`':
    tempStringArray[i] = '-';
    break;
  case '?':
    tempStringArray[i] = '-';
    break;
  case '#':
    tempStringArray[i] = '-';
    break;
  case '{':
     tempStringArray[i] = '-';
    break;
  case '}':
    tempStringArray[i] = '-';
    break;
  case '\\':
    tempStringArray[i] = '-';
    break;
  case '<':
    tempStringArray[i] = '-';
    break;
  case '>':
    tempStringArray[i] = '-';
    break;
  case '*':
    tempStringArray[i] = '-';
    break;
  case '/':
     tempStringArray[i] = '-';
    break;
  case '$':
    tempStringArray[i] = '-';
    break;
  case '!':
    tempStringArray[i] = '-';
    break;
  case '\'':
    tempStringArray[i] = ' ';
    break;
  case '\"':
    tempStringArray[i] = '';
    break;
  case '@':
    tempStringArray[i] = '-';
    break;
  case '+':
     tempStringArray[i] = '-';
    break;
  case '=':
    tempStringArray[i] = '-';
    break;
  case '%':
    tempStringArray[i] = '-';
    break;
}//end of switch
}//end of loop

tabname = tempStringArray.join("");

For the url content in the file I got problem too that I solved even though I'm still confused about it :

  //url gives a bing search using the words of the real title, but if and extra segment of string is added (even '') it is ok...
  //Hypothesis is that the last char was a special unallowed char.
  var theTabURL = currentTab.url + '';
  var stringicon = '';//no icon wanted.
  var txt = '[InternetShortcut]\nURL=' + theTabURL + '\nIDList=\nHotKey=0\nIconFile=' + stringicon + '\nIconIndex=0';
  //subfolder is a global variable and initialized from getFileName(url) of the download request.
  browser.downloads.download({url: URL.createObjectURL(new Blob([txt])), conflictAction: 'uniquify', filename: subfolder + '/' + tabname + '.URL'});

I also solved the "File Downloaded from the Internet" Warning.

First, I tried the Streams solution provided by Windows Sysinternals to edit the file's alternate data streams of information unnamed data stream provided by NTFS file system. https://docs.microsoft.com/en-ca/sysinternals/downloads/streams

It worked, but it wasn't good enough since each file had to be processed.

The real solution I used was to edit the user's Group Policy to prevent NTFS file system to write the "downloaded from internet" data into the file.

https://www.404techsupport.com/2016/06/24/unblock-files-powershell/

"You can disable the setting through Group Policy by enabling the setting ‘Do not preserve zone information in file attachments’ found under User Configuration -> Administrative Templates -> Windows Components -> Attachment Manager."

Note that I had to do it to each user and that for non-admin user I had to set them admin, save and set them back to normal user.

I tested it a few times and the warning is gone.

At that point I was done with the solution to this problem since it worked.

However, for some reason, it consumes a lot of CPU. I'm not sure if it is because of performance related to my code or if it is because there is too much task at once.

The work relating to the initial question is about implementing a new feature on an existing tool and before that the tool worked perfectly without performance issue.

I suspect that it relates to the process of the string or the access of the tab content.

Ha, yes... another last thing... about accessing the tab content, I've seen that if the tab was closed or changed before the end of the process, the data of the new tab would be used instead of the data from the initial tab where the script was executed and therefore resulting in a naming problem on the folder where the file would be saved.

I solved that as, currently, a user problem of "going too fast" so there is a minimum of one second wait each time the script is executed to that the script can process properly that name of the save location and other data.

Thanks!

Čítať túto odpoveď v kontexte 👍 0

Všetky odpovede (7)

more options

You may be getting an HTML file (.html) and not an internet shortcut (.url).

You can try the Add-ons forum on Mozilla Discourse.

Upravil(a) cor-el dňa

more options

Let me see whether I understand. Every time your extension downloads a file, you want to download both:

  • the actual file
  • a valid Windows .url file with the download address (or the source page address?)

The icon looks like some extra work. When I look at a .url file created using drag-and-drop, the IconFile points to a special folder where Firefox stores icons for shortcuts:

[InternetShortcut]
URL=https://support.mozilla.org/en-US/questions/1337215
IDList=
HotKey=0
IconFile=C:\Users\<username>\AppData\Local\Mozilla\Firefox\Profiles\<profile.folder>\shortcutCache\+JfTdPjVLSa0JnjuMe45nQ==.ico
IconIndex=0

Extensions are not allowed to save outside of the Downloads folder, so your extension could download the icon to a shortcutCache folder in the Downloads folder and set the IconFile to an address there. But getting the right icon at a good size, well, it seems a bit complicated.

more options

Hello jscher2000,

Yes, the goal is to get the actual file and the Windows .url file, both in the download folder.

I was not aiming for the direct url of the downloaded file, but more from its source page, but this will be decided more in the code itself since the url of the source page and the url of the downloaded file itself will be available. Simply put, the goal is to have the Windows .url file from a url address.

I've taken a look at the IconFile and that makes perfect sense. I also noticed that sometimes the icon is not there. I don't really care about the icon, so I'll drop the idea to get the icon.

The icon being saved in a user folder would also make sense as to why the drag-drop url file creation feature is not available to the code as it saves outside the download folder. I overlooked this information (my bad).

As cor-el mentioned, the other add-ons I've seen may have been other file format that can contain an image (although I'm not sure to remember if the icon was displayed in the OS).

My best guess at this point would be to create a Windows shortcut document and fill the information with the url's web page information (no icon), but it's something I also searched and did not find a good answer for a specific shortcut file.

If I still hasn't figured an answer in the next week, I'll try the Add-ons forum on Mozilla Discourse.

Thanks for taking the time to answer this and hopefully we'll find a way to do it.

more options

You can construct your .url file this way, but it is tagged as an executable from the web, so it will trigger that warning:

var url = 'https://www.userchrome.org/samples/userChrome.css';
var fname = 'userChrome.css';
var txt = '[InternetShortcut]\nURL=' + url;
//Trigger download via API (not in content script)
browser.downloads.download({
    url: URL.createObjectURL(new Blob([txt])),
    filename: '[Download] ' + fname + '.url'
});
more options

Hello again,

I have some bad news, other good news and the work is not over yet.

The bad news is that I wasn't able to deal with the security warning. It seems to be specific to Windows and does not concern the generated file.

Solution to this is another topic, but I'll explore this:

https://www.howtogeek.com/70012/what-causes-the-file-downloaded-from-the-internet-warning-and-how-can-i-easily-remove-it/

I've looked at some other regular downloaded files and they also have that security flag activated, however I never seen the warning.

I find it kind of stupid from Windows to detect the file was downloaded from internet considering that is was created locally, but I guess it can make sense since it passes to the browser download.

I don't know if this problem would occur with an installed software.

Anyway. For the good news, I was able to generate the .url file.

Not only that, but I found a way to support the icon even though it's an inconvenience to do.

I looked at the different files that are created with the drag and drop feature and realized there was an additionnal field of "web document" that was supporting the icon.

When writing the content of the file, it's possible to add those properties and the .url file will take them in account.

I haven't tested if it was possible to set a relative path for the icon location, but I don't think I'll support the feature since it'll cause to manage an extra file each time.

As a result, here is the code :

var stringicon = 'my local file path to the image location'; var url = 'https://www.userchrome.org/samples/userChrome.css'; var txt = '[InternetShortcut]\nURL=' + url + '\nIDList=\nHotKey=0\nIconFile=' + stringicon + '\nIconIndex=0'; var fname = 'file name';

//Trigger download via API (not in content script). browser.downloads.download({url: URL.createObjectURL(new Blob([txt])), filename: fname + '.URL'});

That's good progress, but I'm not done yet.

The last thing that I want to do is set the correct file name to be the same as the name from the drag and drop feature.

That name is not the name of the downloaded file, but the name of the page.

If I remember correctly that name comes from the description of the web page entered by the programmer.

I feel detecting and retrieving this information is going to be hard work.

I really don't know where to look for that or even if it is possible in a add-on.

Anyone with an idea is welcome.

more options

The page title is do-able:

  • If you are injecting a content script, your script can get the page title using document.title (normal DOM).
  • If you are using only a background script but have tabs permission, your background script can get the page title from the tab object.
more options

Vybrané riešenie

I simply wanted to do a follow up and say that I found the solution to all of the concerns.

I'm using a background script with all the appropriate permission.

I ran into some trouble getting access to the data and creating the file.

Creating the file was a problem because there were unsupported characters by Windows in the file's name.

For some reason I was getting a bing url when the name was not allowed and that was very confusing.

I solved by I implementing a for loop to parse the string of the file name and replace the chars causing troubles. I was able to reproduce the same name as the drag and drop features (with the exception that Instead of loosing some chars for a space I put a '-' instead.

Here's the code sample I wrote :

  //title gives a bing search using the words of the real title.
  //Seems like it is related to the fact that the title's string contains unsupported chars by windows OS for file name.
  var tabname = currentTab.title;

  //To be a valide file name, some forbidden chars must be removed from the title :
  //# % & { } \ < > * ? / $ ! ' " : @ + ` | =
  //replace it with '-' to keep some of the layout's structure in the name

tabname = tabname.trimLeft();

//In JavaScript, strings are immutable.
//which means the best you can do is to create a new string with
//the changed content and assign the variable to point to it.

//There is no replaceAt function in JavaScript.

var tempStringArray = tabname.split("");
var tempStringArrayLength = tempStringArray.length;
for (var i = 0; i < tempStringArrayLength; i++) {
switch (tempStringArray[i]) {
  case ':':
    tempStringArray[i] = '-';
    break;
  case '|':
    tempStringArray[i] = '-';
    break;
  case '`':
    tempStringArray[i] = '-';
    break;
  case '?':
    tempStringArray[i] = '-';
    break;
  case '#':
    tempStringArray[i] = '-';
    break;
  case '{':
     tempStringArray[i] = '-';
    break;
  case '}':
    tempStringArray[i] = '-';
    break;
  case '\\':
    tempStringArray[i] = '-';
    break;
  case '<':
    tempStringArray[i] = '-';
    break;
  case '>':
    tempStringArray[i] = '-';
    break;
  case '*':
    tempStringArray[i] = '-';
    break;
  case '/':
     tempStringArray[i] = '-';
    break;
  case '$':
    tempStringArray[i] = '-';
    break;
  case '!':
    tempStringArray[i] = '-';
    break;
  case '\'':
    tempStringArray[i] = ' ';
    break;
  case '\"':
    tempStringArray[i] = '';
    break;
  case '@':
    tempStringArray[i] = '-';
    break;
  case '+':
     tempStringArray[i] = '-';
    break;
  case '=':
    tempStringArray[i] = '-';
    break;
  case '%':
    tempStringArray[i] = '-';
    break;
}//end of switch
}//end of loop

tabname = tempStringArray.join("");

For the url content in the file I got problem too that I solved even though I'm still confused about it :

  //url gives a bing search using the words of the real title, but if and extra segment of string is added (even '') it is ok...
  //Hypothesis is that the last char was a special unallowed char.
  var theTabURL = currentTab.url + '';
  var stringicon = '';//no icon wanted.
  var txt = '[InternetShortcut]\nURL=' + theTabURL + '\nIDList=\nHotKey=0\nIconFile=' + stringicon + '\nIconIndex=0';
  //subfolder is a global variable and initialized from getFileName(url) of the download request.
  browser.downloads.download({url: URL.createObjectURL(new Blob([txt])), conflictAction: 'uniquify', filename: subfolder + '/' + tabname + '.URL'});

I also solved the "File Downloaded from the Internet" Warning.

First, I tried the Streams solution provided by Windows Sysinternals to edit the file's alternate data streams of information unnamed data stream provided by NTFS file system. https://docs.microsoft.com/en-ca/sysinternals/downloads/streams

It worked, but it wasn't good enough since each file had to be processed.

The real solution I used was to edit the user's Group Policy to prevent NTFS file system to write the "downloaded from internet" data into the file.

https://www.404techsupport.com/2016/06/24/unblock-files-powershell/

"You can disable the setting through Group Policy by enabling the setting ‘Do not preserve zone information in file attachments’ found under User Configuration -> Administrative Templates -> Windows Components -> Attachment Manager."

Note that I had to do it to each user and that for non-admin user I had to set them admin, save and set them back to normal user.

I tested it a few times and the warning is gone.

At that point I was done with the solution to this problem since it worked.

However, for some reason, it consumes a lot of CPU. I'm not sure if it is because of performance related to my code or if it is because there is too much task at once.

The work relating to the initial question is about implementing a new feature on an existing tool and before that the tool worked perfectly without performance issue.

I suspect that it relates to the process of the string or the access of the tab content.

Ha, yes... another last thing... about accessing the tab content, I've seen that if the tab was closed or changed before the end of the process, the data of the new tab would be used instead of the data from the initial tab where the script was executed and therefore resulting in a naming problem on the folder where the file would be saved.

I solved that as, currently, a user problem of "going too fast" so there is a minimum of one second wait each time the script is executed to that the script can process properly that name of the save location and other data.

Thanks!

Upravil(a) cor-el dňa