-1

What I want it to do is, every time I visit a new page, click a link, etc. the URL will automatically save to a .txt file. Either Chrome or Firefox is okay. PHP, HTML, Java, Javascript is okay too. If anyone can help me it would be awesome.

chrome.browserAction.onClicked.addListener(createFile);
createFile();

function createFile()
{
    chrome.tabs.getSelected(null, function(tab) {
        window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
            fs.root.getFile('test', {create: true}, function(fileEntry) {
                fileEntry.createWriter(function(fileWriter) {
                    var builder = new WebKitBlobBuilder();
                    builder.append("Saurabh");
                    builder.append("\n");
                    builder.append("Saxena");

                    var blob = builder.getBlob('text/plain');

                    fileWriter.onwriteend = function() {
                        chrome.tabs.create({"url":fileEntry.toURL(),"selected":true},function(tab){});
                    };
                    fileWriter.write(blob);
                }, errorHandler);
            }, errorHandler);
        }, errorHandler);
    });
}
function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  Console.Log('Error: ' + msg);
}

I already tried that Javascript code using Tampermonkey in Chrome, but it didn't work.

  • 1
    Maybe you should try to write a browser plugin that will be watching for URL that you opens. – Dmytro Plekhotkin Sep 06 '14 at 16:45
  • This looks really suspicious.. `WebKitBlobBuilder` is obsoleted, you probably want to use the _Blob_ constructor; `new Blob([data], {type: "text/plain"})` – Paul S. Sep 06 '14 at 16:48
  • Here is an answer for your question: http://stackoverflow.com/questions/19802032/how-can-a-chrome-extension-save-many-files-to-a-user-specified-directory – Dmytro Plekhotkin Sep 06 '14 at 19:11
  • without an extention, you have to create a new file each time you have a new url. you can side-skirt that by collecting the urls in localStrorage on a specific domain, then downloading the list on-demand. or, you can save all those single files to a certain folder and append them all together at once on-demand, or use a certain executable to handle them and have that app append your master list. – dandavis Sep 06 '14 at 19:52

1 Answers1

0

I know how to save URLs into Chrome storage by creating Chrome extension.

You have to create manifest.json with structure described below:

{
  "name": "Save URLs",
  "description": "Save URLs",
  "version": "0.7",
  "permissions": [
          "tabs", "storage"
  ],
  "background": {
    "scripts": ["store.js"]
  },
  "manifest_version": 2
}

and JavaScript file store.js

var urlList = [];
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.url) {
        urlList.push(tab.url);
        chrome.storage.sync.set({'urlList': urlList}, function() {
            // callback body
        });

        chrome.storage.sync.get('urlList', function(items) {
            alert(items.urlList);
        }); 
    }
});

Information about loading the extension into browser: https://developer.chrome.com/extensions/getstarted#unpacked

Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47