11

I want to read and display the contents of file in a chrome extension (The file is already inside the extension directory).How do it?Whether I can use HTML5 to read it?

 var elema3 = document.body.getElementsByClassName("slicefooter"); elema3[0].innerHTML='Shanmuga Subramanian'; var a1=chrome.extension.getURL('script1.txt'); var reader = new FileReader(); reader.readAsText(a1); 

2 Answers 2

14

Use XMLHttpRequest.

Example:

var xhr = new XMLHttpRequest(); xhr.open('GET', chrome.extension.getURL('script1.txt'), true); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { //... The content has been read in xhr.responseText } }; xhr.send(); 
Sign up to request clarification or add additional context in comments.

Comments

1

A better option would be to store the contents of your text in your HTML itself using something like

<script id="textFile" type="text/x-template">...</script>

and then reference the contents of the template via document.getElementById('textFile').

Let me know if you need more information.

2 Comments

this is not applicable to chrome extensions
It totally does, in fact I should have thought of this before!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.