Extracting text from a web page and save it on a local drive using chrome native messaging

9,189 views
Skip to first unread message

Peter Chan

unread,
Nov 19, 2014, 10:50:01 PM11/19/14
to chromium-...@chromium.org

The project I am working on requires me to extract a few paragraph of text (ie: from textarea or div tag) from a regular web page and save them to my local drive (preferably Windows). (The paragraphs of text stored on my local drive will then be read by another native program. So storing the paragraphs of text on my local drive within a “sandbox” may not be of much help) .


Is it possible to do that at all?


I have tried learning/using Chrome extension/app, Chrome native messaging and Node.JS. So far, correct me if I am wrong, I see that users can send/save (1) the text from a regular web page to Chrome extension (and not to Chrome app), (2) text from Chrome app (and not from Chrome extension) to local drive and (3) text from either Chrome app or extension to  local drive using node.js.


So my plan is to write a Chrome extension and a javascript to run under node.js (ie: hosting locally). The Chrome extension will extract text from the regular web page then sent it to the javascript (running under node.js on the local machine). Upon receiving the text sent from the Chrome extension, the javascript will save the text on to the local drive.


ie: WebPage → Chrome extension → c:\>node c:\abc\myScript.js → localDrive


Is it possible to do that at all? Are there easier/better ways? Can I get away without using node.js (meaning saving directly from Chrome extension/app)? I guess, NaCl may not help, as I understand it sandboxed everything it stored on local drive.


All I need is to be able to store text from a regular web page on to my local drive.


Thanks

Wolf War

unread,
Nov 20, 2014, 5:40:58 AM11/20/14
to chromium-...@chromium.org
you can save txt file directly from extension

// collect your text (from divs, textarea..etc) into variable and stringify it
          var myText;
         
var myStrText=JSON.stringify(myText);



// then call a function with arguments (name, data)
         saveText("default_name.txt", myStrText);



// function to save it to HDD (default download folder or with prompt) is like this
         function saveText(filename, text) {
           
var tempElem = document.createElement('a');
            tempElem
.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
            tempElem
.setAttribute('download', filename);
            tempElem
.click();
         
}
Message has been deleted

Peter Chan

unread,
Nov 21, 2014, 4:50:20 AM11/21/14
to chromium-...@chromium.org
Thanks Wolf war, I cut and paste the script and it works right away!!!!

One more thing: I would like to forcefully save the file (ie: default_name.txt) to c:\abc\default_name.txt, (1) without prompting the users with file-chooser dialog box (2) regardless of whether default_name.txt has already existed or not and (3) regardless of whether the directory "c:\abc" has already existed or not.

Also how would I save an image (from img tag) to a local drive?

thanking you in advance

Wolf War

unread,
Nov 22, 2014, 9:11:47 AM11/22/14
to chromium-...@chromium.org
no, sry...I don't think you can "silently" save something on user HDD in specified folder. User action must be involved.
It's either with the save dialog prompt, or if user unchecked that box in chrome settings "ask where to save each file before downloading", then your extension will save file directly without asking into default download folder and increment file name +1.
But you cannot choose for the user where to save it (specific path) and force download into it.
Security reasons.

about image...
you can use chrome.downloads API (requires permission "downloads" in manifest file, see the top of the page I linked)

as far as I get it, just get image src url into some "imgSrc" variable and use it like this
chrome.downloads.download({ url: imgSrc, saveAs: false });

hope this helps

Peter Peter

unread,
Nov 22, 2014, 7:47:58 PM11/22/14
to Wolf War, chromium-...@chromium.org
Thanks


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/a13d55bb-a9bd-4b04-93f5-55ac2faff718%40chromium.org.

Peter Peter

unread,
Nov 22, 2014, 10:00:21 PM11/22/14
to Wolf War, chromium-...@chromium.org
Hi WW,

What is your idea, if I do this:
(1) unchecked  "ask where to save each file before downloading" (to save silently) 
(2) then just before saving the file onto the local drive I would delete (using javascript, not manually) the local version. 

Would it allow me to save the file silently on to the local drive and without adding +1 version to my file name ?
Would chrome allow me to delete the local file? If so would you suggest me on how I would delete the local file.

Thanking you in advance.

Wolf War

unread,
Nov 23, 2014, 6:37:17 AM11/23/14
to chromium-...@chromium.org, wolf...@gmail.com
there is a chrome.downloads.onDeterminingFilename.addListener and as explained here, you can use it like :

chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
  suggest
({filename: item.filename,
  conflict_action
: 'overwrite',
  conflictAction
: 'overwrite'});
});
I don't know if its gonna work out of the box, din't test it, but this will make rule for all downloads with the same name to be overwritten (as long as extension is enabled)
not just your txt file
for instance, if user downloaded image with the name photo.jpg, and then later downloads the same image or different with the same name, that image will be overwritten


Peter Peter

unread,
Nov 24, 2014, 1:55:32 AM11/24/14
to Wolf War, chromium-...@chromium.org
HI WW,

looks promising; I will try it out and let you know.

Thanks

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

Joey Cham

unread,
Mar 22, 2018, 3:18:22 AM3/22/18
to Chromium-Extensions-Announce
Hi Wolf War and others,

I'm a newbie for Chrome extension. I'm here to learn from experts, like all of you here.

I'm trying to follow the scripts above to generate a text file for downloading in my Chrome extension. It works excellent! But when the Chrome was updated today, the download no longer works - No error prompt in the background console, the script works fine before and after the saveText function, just no download. I tried the same code in other computers with older version of Chrome, it still works.

I've spent hours on this issue. Any expert here can give me some hints? Any idea will be highly appreciated. Thank you very much!

Joey 
Reply all
Reply to author
Forward
0 new messages