Skip to content Skip to sidebar Skip to footer

Chrome Plugin Reading Text File On Hard Drive And Replacing Textbox Content

I'm doing a lot of work where I use sublime to code and then need to copy the contents to a web based 'editor' that really sucks. I would like the contents of this textbox/editor t

Solution 1:

The easiest way to get the contents of a local file in an extension is probably through XMLHttpRequest. For example, the following background script gets the content of /etc/passwd and prints it to the background console:

functionpollContent() {
    var x = newXMLHttpRequest();
    x.open('GET', 'file:///etc/passwd');
    x.onload = function() {
        // Do something with data, e.g. print to consoleconsole.clear();
        console.log(x.responseText);

        setTimeout(pollContent, 5000);
    };
    x.send();
}
pollContent();

In order to use the previous function, you have to declare the file://*/* permission (or at least file:///etc/passwd) and enable the "Allow access to file URLs" option for your extension at chrome://extensions/.

Only the background page can get file:// access. You need to use the message passing API to put the content in a textbox with a content script.

Post a Comment for "Chrome Plugin Reading Text File On Hard Drive And Replacing Textbox Content"