I want to convert the plaintext Public Suffix List to JSON object so as to use it in my script. I just want the list of TLD's.
The list is here https://publicsuffix.org/list/effective_tld_names.dat
My initial thought was to regex match the file for suffix list but I don't know how to regex files in javascript. I'm kind of new to javascript.
Anyone having idea, how to achieve this. Thank You
First off, the list doesn't seem to be allowed to do cross-domain, so direct AJAX to the resource isn't possible.
What you can do is have your server load it for you (PHP: file_get_contents, JS: http.get). Then, implement a parser. I'm not familiar with the format of the file, but you can read the file line by line, skip blank lines and lines with //. Then load them into an array (PHP: array_push, JS: Array.prototype.push), serialize (PHP: json_encode, JS: JSON.serialize) and ship as JSON to your app.
Related
I need to load multiple different JSON files.Now I use many AJAX to load them one by one, according to a list of their file name. But if I add a new JSON, I should change the list by myself. How can I load JSON files with code automatically?
solution 1:
A possible solution is to create a JSON file that contains all file names. Then you just have to update the JSON file instead of the list.
solution 2:
In the case where the file names are like file1.json, file2.json, ..., you can try to get them with a loop and leave it at the first AJAX resource not found exception.
I am trying to do a simple task; retrieve JSON using Jquery and display the name to a simple HTML list, usually, the JSON file I deal with is quite straightforward with a format of
[ {a:1,b:2},{a:3,b:4}]
, but this time, the file (hosted on a different website) has a format similar to
[
["John",21,5,"description","some-link"],["Doe",3,6,"description","some-link"],]
with another 100k entries. My goal is to simply display all the names of this file in an HTML list.
Is there a way to retrieve the list and display content such as the name and description in simple HTML ul?
the actual file link is here: https://www.dropbox.com/s/104iutbz95o7wtw/test.json?dl=0 (consider it is actually hosted on a website for example: https://sample.com/test.json)
You need a regex to return a list of the data.
Have a look at this one I made for you: https://regex101.com/r/aDRp6A/1
It would help to know if the data has a strict structure or if it's variable.
EDIT:
Given your sample data I can see yours is just a list of lists. No curly brackets. That's good!
Once you have your json as a string try to split it:
myList = yourJsonObject;
And then you can access the data with:
myList[0][0]
hello you need to build your own parser or use regex, because the format that you mention is not JSON
I am new to web dev and I have a text file that I created using C# to collect some data from a website. Now I want to use that data to make graphs or some way to show the info on a website. Is it possible to use I/O in javascript or what is my best option here? Thanks in advance.
You have several options at your disposal:
Use a server-side technology (like ASP.Net, Node.js etc) to load, parse and display the file contents as HTML
Put the file on a web server and use AJAX to load and parse it. As #Quantastical suggested in his comment, convert the file to JSON forma for easir handling in Javascript.
Have the original program save the file in HTML format instead of text, and serve that page. You could just serve the txt file as is, but the user experience would be horrible.
Probably option 1 makes the most sense, with a combination of 1 + 2 to achieve some dynamic behavior the most recommended.
If you are working in C# and ASP then one option is to render the html from the server without need for javascript.
In C# the System.IO namespace gives access to the File object.
String thetext = File.ReadAllText(fileName);
or
String[] thetextLines = File.ReadAllLines(fileName);
or
If you have JSON or Xml in the file then you can also read and deserialize into an object for easier use.
When you have the text you can create the ASP/HTML elements with the data. A crude example would be:
HtmlGenericControl label = new HtmlGenericControl("div");
label.InnerHTML = theText;
Page.Controls.Add(label);
There are also HTMLEncode and HTMLDecode methods if you need them.
Of course that is a really crude example of loading the text at server and then adding Html to the Asp Page. Your question doesn't say where you want this processing to happen. Javascript might be better or a combination or C# and javascript.
Lastly to resolve a physical file path from a virtual path you can use HttpContext.Current.Server.MapPath(virtualPath). A physical path is required to use the File methods shown above.
I am trying to use docx.js to generate a Word document but I can't seem to get it to work.
I copied the raw code into the Google Chrome console after amending line 247 to fix a "'textAlign' undefined error"
if (inNode.style && inNode.style.textAlign){..}
Which makes the function convertContent available. The result of which is an Object e.g.
JSON.stringify( convertContent($('<p>Word!</p>)[0]) )
Results in -
"{"string":
"<w:body>
<w:p>
<w:r>
<w:t xml:space=\"preserve\">Word!</w:t>
</w:r>
</w:p>
</w:body>"
,"charSpaceCount":5
,"charCount":5,
"pCount":1}"
I copied
<w:body>
<w:p>
<w:r>
<w:t xml:space="preserve">Word!</w:t>
</w:r>
</w:p>
</w:body>
into Notepad++ and saved it as a file with an extension of 'docx' but when I open it in MS Word but it says 'cannot be opened because there is a problem with the contents'.
Am I missing some attribute or XML tags or something?
You can generate a Docx Document from a template using docxtemplater (library I have created).
It can replace tags by their values (like a template engine), and also replace images in a paid version.
Here is a demo of the templating engine: https://docxtemplater.com/demo/
This code can't work on a JSFiddle because of the ajaxCalls to local files (everything that is in the blankfolder), or you should enter all files in ByteArray format and use the jsFiddle echo API: http://doc.jsfiddle.net/use/echo.html
I know this is an older question and you already have an answer, but I struggled getting this to work for a day, so I thought I'd share my results.
Like you, I had to fix the textAlign bug by changing the line to this:
if (inNode.style && inNode.style.textAlign)
Also, it didn't handle HTML comments. So, I had to add the following line above the check for a "#text" node in the for loop:
if (inNodeChild.nodeName === '#comment') continue;
To create the docx was tricky since there is absolutely no documentation on this thing as of yet. But looking through the code, I see that it is expecting the HTML to be in a File object. For my purposes, I wanted to use the HTML I rendered, not some HTML file the user has to select to upload. So I had to trick it by making my own object with the same property that it was looking for and pass it in. To save it to the client, I use FileSaver.js, which requires a blob. I included this function that converts base64 into a blob. So my code to implement it is this:
var result = docx({ DOM: $('#myDiv')[0] });
var blob = b64toBlob(result.base64, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
saveAs(blob, "test.docx");
In the end, this would work for simple Word documents, but isn't nearly sophisticated for anything more. I couldn't get any of my styles to render and I didn't even attempt to get images working. I've since abandoned this approach and am now researching DocxgenJS or some server-side solution.
You may find this link useful,
http://evidenceprime.github.io/html-docx-js/
An online demo here:
http://evidenceprime.github.io/html-docx-js/test/sample.html
You are doing the correct thing codewise, but your file is not a valid docx file. If you look through the docx() function in docx.js, you will see that a docx file is actually a zip containing several xml files.
I am using Open Xml SDK for JavaScript.
http://ericwhite.com/blog/open-xml-sdk-for-javascript/
Basically, on web server, I have a empty docx file as new template.
when user in browser click new docx file, I will retrieve the empty docx file as template, convert it to BASE64 and return it as Ajax response.
in client scripts, you convert the BASE64 string to byte array and using openxmlsdk.js to load the byte array as an javascript OpenXmlPackage object.
once you have the package loaded, you can use regular OpenXmlPart to create a real document. (inserting image, creating table/row ).
the last step is stream it out to end user as a document. this part is security related. in my code I send it back to webserver and gets saved temporarily. and prepare a http response to notify end user to download it.
Check the URL above, there are useful samples of doing this in JavaScript.
I want to load an external file using AJAX GET and then parse it for the relevant information on it leaving out all the comments.
file: stuff.conf
: This is the list
: of colors needed
#5d3939 : nice
#9e1818 : ugly!
#cd7979
#409c81
#6e6f14 : ok...
I want the hex colours in an array.
Please help!
Here you go:
var arr = response.match(/\#[a-f0-9]{6}/gi);
where response is your Ajax response string.
Live demo: http://jsfiddle.net/simevidas/RnPS3/1/
You can write your own JS to parse any type of data format. But, the somewhat standard way to interchange data like this with the least hassle is to put the data in the JSON format with the color values in an array (or whatever format you want them to end up on). You then read the contents of the file into a string variable and then call a JSON parser. The return value of the parser will be an array of color values (if that's how you format the JSON). The latest browsers have JSON parsers built in. For cross-browser compatibility with older browsers, you can either use a parser in one of the common libraries like jQuery or YUI or find code on the web to add just a JSON parser.