Pass data from chrome extension to .net app - javascript

I'm trying to send data from my chrome extension to my .net application using ajax. I'm using background script to send data. Currently i'm unable to get data at my server. I guess there's issue in setting up manifest for chrome. However, how can i post data from chrome extension?
Suggest any other alternatives if possible.
Thank you.

You can send data to the server using XHR, or use jQuery.ajax() if you prefer. The end point will be the web service you have defined on the server. Check out this example, which uses jQuery for it.
For posting data, you pass all the data you want from the client in JSON format. You can use JSON.stringify() to convert your JavasScript object to a JSON string. If your object matches an entity structure on the server, it should automatically populate it, allowing you to specify that entity as the parameter of the web method. Otherwise, you can accept an object parameter and extract the data from that.
In a Chrome extension, make sure you have the correct cross-origin permissions.

There is a specific mechanism in Chrome for communication with local applications: Native Messaging.
There is an important limitation to keep in mind: Chrome cannot talk to an already-existing process; it can only start a new one and talk over STDIO.
So you may need to have a "proxy" process that Chrome can start, which will connect (somehow, but no longer restricted in methods) to your main app and relay data.

Related

Caching data locally (in browser) received using Socket.io?

Is there a library/project which smooths over the process of caching some json data in the browser when using socket.io? I guess what I really mean is, is there a github/opensource project already out there focused on this task so that a developer could more or less drop it into any socket.io project?
For example, let's say I am getting tabular data for a page and the data is received by using socket.io. I want the data to remain cached so that I can save a server request if the user reloads the browser.
Additionally, I'd want this to happen more or less without me having to manually create cache variables, like: http://davidwalsh.name/cache-ajax . I want the socket.io cache library to be able to do this for me.
I want to occasionally & easily be able to clear the cache if it changed on the server. So, let's assume for that what I'm looking for has a method for analyzing timestamps on when the remote data was modified. What I mean is, let's assume for now that there is a way of notifying the browser when database table/rows/documents have been modified so that it knows when to clear the socket.io cache (perhaps sending meta information about database table modify timestamps along with data requests or maybe with an occasional comet-type message).
Why not use local storage? Read about it from Mozilla Developer Network
It is easier to use.
To set value
localStorage[ 'key' ] = strValue;
To retrieve
strVal = localStorage[ 'key' ];
Yup, just simple associative arrays

How to Stream data from WebAPI method to a JavaScript client

I have a service method written in ASP.Net WebAPI :http://diningphilospher.azurewebsites.net/api/dining?i=12
and JavaScript client gets the response and visualizes it here
But the nature of Dining Philosophers problem is I never know when the Dead-lock or starvation will happen. So Instead of having a request/response I would like to stream the data through service method and client side JavaScript read the data I assume JSON asynchronously. Currently several post directs me towards changing the default buffer limit in WebAPI so you get a streaming like behavior.
what other(easy or efficient) ways exist to achieve this above behavior.
You can return PushStreamContent from ASP.NET Web API and use Server Sent Events (SSE) JavaScript API on the client side. Check out Push Content section in Henrik's blog. Also, see Strathweb. One thing I'm not sure about the latter implementation is the use of ConcurrentQueue. Henrik's implementation uses ConcurrentDictionary and that allows you to remove the StreamWriter object from the dictionary corresponding to the clients who drop out, which will be difficult to implement using ConcurrentQueue, in my opinion.
Also, Strathweb implementation uses KO. If you don't like to use KO, you don't have to. SSE JavaScript APIs have nothing to do with KO.
BTW, SSE is not supported in IE 9 or lesser.
Another thing to consider is the scale out option. Load balancing will be problematic, in the sense there is a chance that the load will not be uniformly distributed, since clients are tied to the server (or web role) they hit first.

Protecting JSON in Javascript?

I used jQuery Ajax to talk to php script, then it returns JSON. Then, the returned JSON Array Object is assigned to Javascript Variable var myJSON = ajaxReturn;
Normally the returned JSON values are not visible in Page Source or Javascript File, as it is rendered on the runtime only.
But when i open the tools like, Firebug and call that variable in console, like: alert(myJSON); the results are popping out. I do NOT want it to be as this is something secret data.
What is the best way to prevent/protect the JSON at Javascript side?
Everything sent to the client side is public, this is the nature of front end development and you can't change it. It is impossible to hide stuff from the user if he decides to take a peek.
If the purpose of your application is to store the JSON for client use, then you have no way of protecting it from being accessed. However you can do all modifications upon receiving the JSON and then discard it (not store it). Keep in mind that the request can still be intercepted the response can be read simply by using the networking tab of the browser developer tools.
What do you do with the JSON data? In all probability, you are feeding UI controls or subsequent calls to web services. So if you would protect (i.e., encrypt) the JSON, you would still need client-side decryption, and so your JSON would still be vulnerable -- as you can just do an alert(decryptedJSON) too.
There is no real, safe way to protect JSON if you have to be able to decipher the data in the browser.
You can of course protect the data while it is underway over the network by encrypting it, either using HTTPS or by explicitly encrypting the data server-side and then decrypting it using client-side JavaScript. But that does not protect it from being viewed in the browser.
A better option could be to encrypt and decrypt only on the server, if that fits your scenario. So you can get encrypted JSON data from a particular web service call, then feed that data into your next web service call where it gets decrypted on the server. That way, your client-side JavaScript doesn't need to decrypt, making your data safer. But if the purpose is to populate the UI, obviously this won't fit your needs.
You have just missed the game, Once you send the data from your server then its out of your limit. Because browser like firefox can do anything, So the point is everything which renders on the client is Public.
Even if there were some way to block Firefox from displaying the data in firebug, its easy for anyone to write their own web client that pretends to be a web browser and then they can do whatever they want with the data.
If you really want to hide json-data then dont send it using ajax-json. Use diffrent terminology or server-side programming.

Call a SOAP Service using JavaScript

As the title suggests, I'm trying to figure out how to call a soap service using JavaScript. The SOAP Service is being run on Tomcat on my local machine. Along with accessing the SOAP service, I can also download/access/read the WSDL very easily.
I've been hitting my head against the wall for days now with this problem. While researching, I'm come across short examples, but none are comprehensive and detail how to accomplish this from start to end. That, or I've found a solution but it only works for IE or Mozilla.
Any ideas how to call a SOAP web service using Javascript (on major browsers)? Thanks!
(PS: I'm also using jquery. So if you want to accomplish this using some slick jquery tool, please feel free to suggest it)
SOAP is just XML so it should be possible to do this, however I'm not aware of any existing SOAP Clients written in JS
A more common method would probably be:
JavaScript initiates an XHR object which calls to a server-side AJAX helper using either GET/POST parameters, or a JSON encoded object
AJAX helper makes the call to either a Database or Web Service, as appropriate, and outputs required data object(s) as either JSON or XML
JavaScript parses response body (JSON or XML) and takes required action using specified data.
This has a couple of advantages over trying to call WebServices directly from JavaScript:
SOAP XML can be quite complex, which means more data to transfer to and from the client, which may be a slow connection. JSON in particular, but also an XML schema that just has the required data would be a much more economical on data usage
If the WebService requires any kind of authentication the authentication details will be exposed to all visitors.
Web Services are generally used for to standardise communications between separate systems, rather than for communication between the front and back end components of the same Application/Site.

Download file using ajax and webservice

There is this 3rd party webservice. One of the public webmethods available is a GetDocument() method. This method returns a Document object. The Document object has properties for File(byte[]), ContentType(string) ect.
My Question : Can I subscribe to this service using javascript(mootools) + ajax + JSON, return the document object, in this case an excel document, and force the file download?
It is true that typically you cannot initiate a download from JavaScript, but there is a flash component, Downloadify that does enable client side file generation.
So you can serve files for download from HTML/JavaScript.
With that problem solved, you still have the problem of how to get the data that you wish to serve from the source web service.
3rd party implies XSS (cross site scripting) which is a no-no using XmlHttpRequest (Ajax).
A possible solution to this problem could be to use a common hidden IFrame technique to get the data.
Simply have an appropriate (hidden?) form that correctly posts to the web service and point it's action to an hidden IFrame element upon which you are trapping the Load event and parse the data returned.
But current browsers have different levels of security measures that limit your ability to access IFrames with an external source so you are actually stuck here. Sorry to get your hopes up.
The only practical robust way to accomplish what you would like to do is to have a local server side script that can act as a proxy between your HTML/JavaScript and the external web service.
Using such a proxy, you can simply go back to using Ajax to get your data to serve up with Downloadify.
But then, since you are using a server script to get the data, why not just serve the data from the script for download?
These are just my observations on the problem domain you present.

Categories