0

I'm making a google extension that injects a lisbox into html page. I need the items for this listbox to be taken from a local textfile (or any other editable source on local PC).

This is what I have right now:

ItemCode.insertAdjacentHTML(
    'afterend', 
    '<select id="name" name="name">
       <option value="">Your keyword...</option>
       <option value="Elvis">Elvis</option>
       <option value="Frank">Frank</option>
       <option value="Jim">Jim</option>
     </select>'
);

I want this values to be taken from a file keywords.txt which in on the Windows Desktop Location and contains the following:

Elvis
Frank
Jim

Is it possible please? Thanks.

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Dunlin
  • 11
  • 3

2 Answers2

0

Here is the code:

// I have no text file, so I will put static content in a string. But bellow is how to get a text file.
// Get text file:
//
// var client = new XMLHttpRequest();
// client.open('GET', '/foo.txt');
// client.onreadystatechange = function() {
//     var filetext = client.responseText;
// }
// client.send();

var fileText = "Elvis\nFrank\nJim";
var people = fileText.split("\n");
var htmlString = '<select id="name" name="name"><option value="">Your keyword...</option>';
for (person in people) {
    htmlString = htmlString + '<option value="'+people[person]+'">'+people[person]+'</option>';
}
htmlString = htmlString + '</select>';

document.write(htmlString);
Rishi
  • 399
  • 6
  • 23
  • thank you!! But how can I inject it using: ItemCode.insertAdjacentHTML('afterend'... ? – Dunlin Jun 09 '17 at 13:54
  • Do the same thing, except, replace your huge string `''` with htmlString. – Rishi Jun 09 '17 at 13:55
  • I will... The interesting part of it that I still can't take the data from the file... I have a keywords.txt in C:/keywords/keywords.txt. It just simple doesn't work for me.:( – Dunlin Jun 09 '17 at 15:35
  • If you are using mac, right click your file, press get info, and change the permissions to read and write for everybody – Rishi Jun 09 '17 at 15:41
  • Also, check this out: https://stackoverflow.com/questions/14446447/javascript-read-local-text-file – Rishi Jun 09 '17 at 15:41
  • Looks like it not possible because of JS restrictions. I need to use HTML 5 ? – Dunlin Jun 09 '17 at 17:25
  • Yes, you need HTML5 and javascript, otherwise, you can use a backend server like php or jsp, and echo the file into the javascript. – Rishi Jun 09 '17 at 17:27
  • Oh man... Thank you so much for yout help, but the point was how to load the text from the keywords.txt which in on the Windows Desktop Location into this listbox... I was playing all weekend around it and cannot get it done....:((( – Dunlin Jun 12 '17 at 14:38
0

it is not possible, i searched the internet . i don't think this code released. also the code above doesn't work because no matter what you do , the list in the html page will consist of file.txt. thats all you get from this code