Rename a file in chrome before download

Rename a file in chrome before download

rename a file in chrome before download

Gerrit indicates that Google Chrome will soon let you rename files Still, the company has a long way to go before the Android version of. The absolute URL that this download initiated from, before any redirects. then the temporary file is renamed to the target filename, the |state| changes to. How to rename downloaded files on Google Chrome. Steps you can follow to rename downloaded files on Google Chrome. Step 1: First, open.

Rename a file in chrome before download - useful

Plugin to rename downloads in Google Chrome

Project Gutenberg is an online library where you can download e-books for free. But every time I download one, the file has a stupid filename, like so I have to manually rename it to the book's title.

I decided to do something about it and create a plugin so that the file is automatically renamed with the book title followed by the book's authors' names. The goal is to have a file with a name useful for Windows' search function.

In this article, I'll walk you through the development stages and the plugin's code. This is my first Google Chrome plugin and I'm not a javascript developer so suggestions for improvement are welcome.

On a side note, the extension is a lot shorter and simpler than I expected.

Demo

Here is a video of what the plugin achieves. On the video, we can see that I download a book from Project Gutenberg. The book appears in my folder with a gibberish filename. Then, I activate the plugin before re-downloading the same book. The book is automatically downloaded inside the subfolder and the file is named according to the book's title :-)

Turn on the subtitles for a real time text description of what is happening.

Our task

When downloading a book from Project Gutenberg, the usual flow is as follows:

  1. user lands on www.cronistalascolonias.com.ar and search for a book
  2. user clicks on a book and lands on the book's page, for instance www.cronistalascolonias.com.ar
  3. user clicks the download link and a file with a non-semantic filename is downloaded
  4. user leaves the book's page

Our task is to:

  • inspect the book's page at step 2 to find the book's title and store it
  • rename the downloaded file automatically at step 3
  • free up the storage space at step 4.

To do so, we will create a chrome extension.

Creating an extension

To create an extension, we must create a new folder with a json file called . This manifest file is the entry point for chrome to recognize and load the extension.

mkdir extension vim extension/www.cronistalascolonias.com.ar

The minimal content of for chrome to recognize the folder as an extension is the following:

www.cronistalascolonias.com.ar
{"name":"Gutenberg Ebooks Downloader","description":"Automatically rename ebooks downloaded from www.cronistalascolonias.com.ar","version":"","author":"Julien Harbulot","manifest_version":2}

To load the extension into Chrome, proceed as follows:

  1. Open the Extension Management page by navigating to ;
  2. Enable Developer Mode by clicking the toggle switch next to Developer mode ;
  3. Click the LOAD UNPACKED button and select the directory to load the extension into Chrome.

Scraping the book's title

First, we need a way to get the book's title. To do so, we will parse the download page with a content script. A content script is a javascript code that executes when a given page is loaded. It can read details (i.e. html) of the page, make changes to it and pass information to the parent extension.

We need to update the manifest to ask for permission to run the extension on the Gutenberg URL and to let chrome know about our content script. To be able to store the book's title, we also need access to the API:

www.cronistalascolonias.com.ar
{,"content_scripts":[{"matches":["www.cronistalascolonias.com.ar*"],"js":["content_www.cronistalascolonias.com.ar"]}],"permissions":["www.cronistalascolonias.com.ar*","storage"]}

Now, let's create our content script in the file .

If we inspect the source code of a page to download a book, for instance this one, we can see that the title is in a heading with :

www.cronistalascolonias.com.ar
<divclass="header"><h1itemprop="name">The Tragedy of Romeo and Juliet by William Shakespeare</h1></div>

We can fetch it easily in javascript using . Then, we need to store it so that our extension can later access it. For that, we will use the storage API and store the book's title along with the page URL:

content_www.cronistalascolonias.com.ar
// Get the content of the h1 titlevarnameProp=www.cronistalascolonias.com.arelector('[itemprop=name]').textContent;// Set everything to lower case, remove special characters and standardize formatnameProp=www.cronistalascolonias.com.arrCase().replace(/[^a-z ]/gi,'');varfilename=www.cronistalascolonias.com.are(' by ',' - ');// use the storage API www.cronistalascolonias.com.ar({[www.cronistalascolonias.com.ar]:filename},function(){www.cronistalascolonias.com.ar('Book filename is stored as: '+filename);});

We need to ensure that the filename is removed from the storage at some point. The best option I could think of is to remove the entry when we leave the page, since we usually leave the page after we have clicked the download link and the entry is thus no longer required. The code is usual javascript along with another call to the storage API:

content_www.cronistalascolonias.com.ar
// Remove when page www.cronistalascolonias.com.arntListener("unload",function(){www.cronistalascolonias.com.ar([www.cronistalascolonias.com.ar],function(){www.cronistalascolonias.com.ar('Removed '+filename);});});

And that's it! Let's see how to intercept the download and rename the file with this newly constructed filename.

Intercepting downloads

To intercept downloads, we will use a background script. A background script is used to monitor events and let our extension react to them. Here, we will use it to react to the downloading of a file.

First, we need to update our manifest to ask permission to access the API and to register our background script:

www.cronistalascolonias.com.ar
{,"background":{"scripts":["www.cronistalascolonias.com.ar"],"persistent":false},"permissions":[,"downloads"]}

Background scripts should almost always be set to . As the documentation indicates:

The only occasion to keep a background script persistently active is if the extension uses www.cronistalascolonias.com.aruest.

There are many events that we can listen to in the download API. The one of interest here is the onDeterminingFilename event.

According to the doc, each listener must call exactly once to change the filename. If the listener calls asynchronously, then it must return . The template is as follows:

www.cronistalascolonias.com.artener(function(item,suggest){asynchronousstuff{suggest({filename:new_filename});}returntrue;});

We can use the argument to find context about the downloaded file. We will use it to ensure the file comes from Project Gutenberg, and use the referrer URL to fetch the book's title in storage. Here's the full code:

www.cronistalascolonias.com.ar
www.cronistalascolonias.com.artener(function(item,suggest){if(www.cronistalascolonias.com.ar("www.cronistalascolonias.com.ar")==-1){// If the file does not come from www.cronistalascolonias.com.ar, suggest nothing www.cronistalascolonias.com.art({filename:www.cronistalascolonias.com.arme});}else{// Otherwise, fetch the book's title in www.cronistalascolonias.com.ar([www.cronistalascolonias.com.arer],function(result){if(result[www.cronistalascolonias.com.arer]==null){// and if we find don't find it, suggest nothing www.cronistalascolonias.com.art({filename:www.cronistalascolonias.com.arme});www.cronistalascolonias.com.ar('Nothing done.');}else{// if we find it, suggest www.cronistalascolonias.com.art=www.cronistalascolonias.com.ar('.').pop();varnewFilename="gutenberg/"+result[www.cronistalascolonias.com.arer]+"."+fileExt;suggest({filename:newFilename});www.cronistalascolonias.com.ar('New filename: '+newFilename);}});// Storage API is asynchronous so we need to return truereturntrue;}});

An icon for our extension

In the page, click the reload icon in the extension's pane. Try downloading an e-book, it should be automatically renamed and put in the subfolder . Yeay!

The last step is to provide a nice icon for the extension. Download one somewhere on the internet, put it in the extension folder and specify its name in the manifest as follows:

www.cronistalascolonias.com.ar
{,"icons":{"":"iconpng"}}

You can use different icon sizes. For more information see the doc here.

Источник: www.cronistalascolonias.com.ar

Removed: Rename a file in chrome before download

Minecraft demo download free
Cv templates for free download
Can you download emcat to pdf
rename a file in chrome before download

Rename a file in chrome before download - pity, that

Rename a file in chrome before download

3 thoughts to “Rename a file in chrome before download”

Leave a Reply

Your email address will not be published. Required fields are marked *