1

What I'm trying to do is rather simple, but I haven't found any streamlined way to do it.

So far what I want is to listen to a part of the content of a webpage and, whenever it changes, save the new content of it into a text file (and overwrite if it already exists). I need that .txt file so it can be accessed in another program whenever it is changed.

I was doing it with a chrome extension until I realized it doesn't allow us to save to the filesystem and the only other way to do it I found was to cross-message it to an chrome app and then save there. It does sounds like a lot of hassle to make an app to read the message, pack it and then get the ID for the extension messaging.

So my question is, how can I automatically save the data from the DOM into a file every time it changes? Or save a message I send through an extension without having to also make an app for it?

Keep in mind it's something for personal use, I don't really care for running it in debug-mode/permissions/etc.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Jim-168
  • 9
  • 5

2 Answers2

0

A MutationObserver is probably what you're looking for, coupled with event.target.innerHTML or some permutation.

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

For the saving of data, I'd recommend checking this Q&A out: Chrome extension: How to save a file on disk

Community
  • 1
  • 1
probablyup
  • 7,636
  • 1
  • 27
  • 40
  • Yes, I checked both links already, my biggest issue is that the user (me) shouldn't have to go back to the website ever to download the file. It should update the file as soon as the site got edited on it's own. – Jim-168 Jan 27 '16 at 01:01
  • One of the answers said that Chrome could programmatically auto download using the Downloads API, if the "ask every time to download" feature was disabled in the browser. – probablyup Jan 27 '16 at 14:56
0

You could use a Mutation Observer and offer a download in the event handler.

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    download('changed.txt', mutation.target.textContent);
    // TODO might need some conversion here ^^
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();
Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • Just one question, wouldn't offer a download need user input? – Jim-168 Jan 27 '16 at 00:54
  • @Jim-168: Nope. If you `setInterval` the download function every 5 seconds, Chrome will prompt to allow the page to download multiple files. This is indeed a problem if you really need to overwrite the same file, but you can timestamp the file, you'll have a history of the changes and can delete the old versions externally. – Dan Dascalescu Jan 27 '16 at 02:37
  • Yeah, I really need it. What I'm trying to do is to get info of what is playing now at my web browser and display it with my streaming program. It only accepts one file as input. Every time the song/video changes it should change the name for the viewers too. – Jim-168 Jan 27 '16 at 02:46