I'm playing about creating an RSS reader widget using Konfabulator/Yahoo. At the moment I'm
pulling in the RSS using
var xmlDoc = COM.createObject("Microsoft.XMLDOM");
xmlDoc.loadXML("http:foo.com/feed.rss");
I've simplified it here by removing the error handling, but what else could I use to do the same task using konfabulator? And how cross platform is this?
COM is Windows-specific, and Yahoo Widgets has XML parsing built-in; so stay away from MSXML :P
You should use the built-in XMLDOM object instead. But since you want to download the XML document from the ’net anyway, XMLHttpRequest supports getting a DOMDocument directly, without having to pass the data to XMLDOM:
var request = new XMLHttpRequest();
request.open( "GET", "http://www.example.com/feed.rss", false);
request.send();
var xmlDoc = request.responseXML;
It works exactly like the XMLHttpRequest on a browser.
For completeness, if you need to parse XML from a string:
var xmlDoc = XMLDOM.parse("<foo>hello world</foo>");
Related
I need to run javascript code from a PDF to post data to an HCL Leap application. Is there anyway in which I can use xmlhttprequest within adobe, or does anyone have a suggestions on modifying to ajax or similar?
Tried having the javascript install npm install xmlhttprequest
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var jp = {};
jp.flowState = 'ST_PendingPDFupload';
jp.pressedButton = 'S_Submit1';
jp.F_Value1 = this.getField("F_Value1").value.toString();
var s = JSON.stringify(jp); //covert to string to send in XHR
var UID = this.getField("UID").value.toString();
var urla = 'https://domain/forms-basic/secure/org/data/ba48a093-5a1d-40ef-
87f6-bb196b95fa9f/F_NewForm1/'
var urlb = '?freedomIdentifyKey=XX'
var url = (urla + UID + urlb)
var client = new XMLHttpRequest(); //open an HTTP request
client.open("PUT",url,false);
client.setRequestHeader('Content-Type', 'application/json');
client.setRequestHeader('Accept', 'application/json');
client.setRequestHeader('Cookie', 'freedomIdentifyKey=XX');
client.send(s);
ReferenceError: require is not defined
You reference Adobe in your question but not Acrobat specifically. If your PDF is being viewed in Adobe Acrobat or Reader you can use Acrobat JavaScript... which is quite different from Browser JavaScript; it uses the same JavaScript Core but has a different object model.
Look at the Net.HTTP.request method at the URL below. The Net.HTTP object allows access to web services that use HTTP. However, you will need to install a folder level JavaScript into each copy of Acrobat that needs to use this method for security reasons.
https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FNet_HTTP_methods.htm%23TOC_requestbc-1&rhtocid=_6_1_8_50_0_0
I'm trying to read a local file with javascript and a Google Chrome App (it may not be possible through Chrome I think), but I can't see what's the latest approach to the problem.
I can read it with the following code:
obj.read = function() {
return new Promise(function(resolve){
var xmlhttp = new XMLHttpRequest();
var file_path = 'file_name.xml';
xmlhttp.open('GET', file_path, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let xml = xmlhttp.responseText;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml");
console.log(xml);
console.log(xmlDoc);
resolve(xmlDoc);
}
}
});
};
But it is like I should be using something like
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
console.log(e.target.result);
};
})(file_path);
var file_path = 'file_name.xml';
var file_parts = [
new Blob(['you construct a file...'],{type: 'text/plain'}),
'Same way as you do with blob',
new Uint16Array([33])
];
var file = new File(file_parts, file_path);
reader.readAsText(file);
(copied from https://stackoverflow.com/a/24495213/826815)
(I'm having a hard time finding literature on this topic)
So, what's the way to do it?
The first approach (XMLHttpRequest for a file in the package) is perfectly valid and likely to be easier if all you need is the whole file.
What you call a "second approach" is lifted from a question about instantiating a File object, not reading an actual file. This would not allow you to read an existing file, just create a "virtual" file that can be used in DOM forms.
There's the whole HTML FileSystem API, which is not implemented anywhere but Chrome and as such documentation is scarce and fraught with big scary red warnings. You can actually use it for the purpose you state - reading App's own files - by starting with chrome.runtime.getPackageDirectoryEntry, but that's probably overkill in your particular case and again, you'll have a hard time finding examples of doing so.
I can think of only two reasons to disturb the slumber of the beast that is FileSystem API for App's own files:
You don't know, at runtime, what files are included. Then you can use it to list files. Rare, but possible.
You need a more fine-grained access than "get me the whole thing", for example you only need a chunk of a large file.
Note that FileSystem API is primarily used in Chrome for chrome.fileSystem Apps API. Considering that Chrome is going to support Chrome Apps on Chrome OS for a while, it's likely to stay, despite being non-standard.
Surviving documentation:
The above-mentioned HTML5Rocks article: Exploring the FileSystem APIs
Last public standards document: File API: Directories and System
Actual standards document on File API (that your "second example" is covered by): File API
I want to retrieve the data contained in a text file (from a given URL) through JavaScript (running on the client's browser).
So far, I've tried the following approach:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
But it only works for Firefox. Does anyone have any suggestions to make this work in every browser?
Thanks
IT works using xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); in IE older versions. Chrome, Firefox and all sensible browsers use xhr
Frankly, if you want cross browser compatibility, use jquery
its pretty simple there:
var text="";
$.get(url, function(data){text=data;//Do something more with the data here. data variable contains the response})
var xhr = new XMLHttpRequest();
xhr.open('POST', '/uploadFile');
var form = new FormData();
form.append('file', fileInput.files[0]);
xhr.send(form);
It was previously impossible to upload binary data with XMLHttpRequest object, because it could not stand the use of FormData (which, anyway, did not exist at that time) object. However, since the arrival of the new object and the second version of XMLHttpRequest, this "feat" is now easily achievable
It's very simple, we just spent our File object to a FormData object and upload it
On an html page I have an <object> that hosts a pdf.
I would need to access the binary data of the pdf via Javascript, but I cannot figure out how
to accomplish that. I get access to the object element itself but cannot think of a method for getting the data in it.
Is it possible at all?
You can not get the binary from an object tag, but you can make an AJAX request to the server and get it as ArrayBuffer by using the new responseType attribute:
var http = new XMLHttpRequest();
http.open("get", "somefile.pdf", true);
http.responseType = "arraybuffer";
http.onload = function(e)
{
if(http.response)
{
// http.response contains the file
}
};
http.send(null);
Note that this method only works in newer browsers and is obviously restricted by the Same-Origin-Policy.
I'm trying to download an HTML page, and parse it using XMLHttpRequest(on the most recent Safari browser). Unfortunately, I can't get it to work!
var url = "http://google.com";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4){
response = xmlhttp.responseText;
var doc = new DOMParser().parseFromString(response, "text/xml");
console.log(doc);
var nodes = document.evaluate("//a/text()",doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
console.log(nodes);
console.log(nodes.snapshotLength);
for(var i =0; i<nodes.snapshotLength; i++){
thisElement = nodes.snapshotItem(i);
console.log(thisElement.nodeName);
}
}
};
xmlhttp.send(null);
The text gets downloaded successfully(response contains the valid HTML), and is parsed into a tree correctly(doc represents a valid DOM for the page). However, nodes.snapshotLength is 0, despite the fact that the query is valid and should have results. Any ideas on what's going wrong?
If you are using either:
a JS library or
you have a modern browser with the querySelectorAll method available (Safari is one)
You can try to use CSS selectors to parse the DOM instead of XPATH.
HTML is not XML. The two are not interchangeable. Unless the "HTML" is actually XHTML, you will not be able to use XPATH to process it.