Receiving data in javascript from the Plugin using NPN_NewStream - javascript

I am trying to send some data as a stream from plug in to javascript. I am using NPN_NewStream for sending the stream from plugin to the browser. I have tried the example mentioned in http://www.terraluna.org/dgp/cvsweb/PluginSDK/Documentation/pi3.htm#npnnewstream successfully. This creates a new stream of HTML text displayed by Netscape in a new window. Now I want to know how I can send some data in the form of xml in a similar way from the plugin to the javascript, receive the data in javascript and do some processing in the javascript with the data sent from the plugin.

I dont' know of a way to do this using NPN_NewStream, and the method for sending HTML to a new window reportedly does not work on all browsers.
However, you can just pass it in a function call as a parameter; make a NPVariant with the text you need and call it into javascript. Alternately have javascript call you and return it from a GetProperty or Invoke call.
FireBreath users do things like this all the time. For more info see http://npapi.com/tutorial3

Related

Asynchronous requests in loop

I want to implement the status log in textarea feature in my WordPress plugin. So I have a PHP variable which constantly updates and I want it to be displayed in the textarea in the admin panel.
I've tried to make a loop in JavaScript which asks server for changes of the variable every second, but as PHP is executing in blocking manner the server just ignores the request.
You can try create a function in your backend who gets all the information and return a HTML object and call this function using Javascript or whatever you wanna use for this call, after you get this html, just update the view.
Or if you prefer using Javascript, rather than returning a HTML, just return all data that you wanna show and update the view :D

Using Javascript to get content from another website

I have a plugin that puts all the content in a database, so I cannot put PHP code into the plugin, it won't execute. It does allow Javascript though.
So how would I go about using javascript to contact a page and get the data, if I have the data just pass the content, like this:
field1=data|field2=data|field3=data|field4=data
like that string.
Is there a way to get that content using javascript or jquery, so that I could parse that and pull the data I need?
I can build a php script to output that data, since I cannot have PHP executed in the plugin, so I could still securely get the data I need, since I could pull the data and make sure to pass a key, so it is secure.

Including a chart and selection box as "widgets" in other webpages?

My data portal offers the possibility to display data as graphs. I managed to give the user the possibility to include these Highcharts graphs via insertion of a Javascript file - which is generated on my side - in their HTML files. [Example]
Now, I'd like to add the possibility to include Dropdown boxes for the selection of countries to be displayed, at the side of the graph. [Example]
However, in order for the user to generate a new graphic with a different set of countries, the request must be send back to our server, and the respond must be send back to the client. I am struggling with how this could be implemented with the least amount of "influence" on the client's site.
JQuery is already integrated in the Javascript file the user inserts into his code. So, I could work with that. Maybe the whole thing only works if the user integrates this in a PHP, and not only an HTML file.
Would this kind of AJAX-call be a possibility:
$("#whatever").load("http://remote-server.com/my_script.php")
I am a bit puzzled about how to continue.
Thanks for any hints!
The best option is communicate with the server via JSON. So you catch event on the dropdown, call $.getJSON() to your server, returning json data, which can be used in the chart.
Functions to manipulate on chart:
- setData
- addPoint
- series.update
All of them are documented here
jQuery has a built in AJAX method that you may find useful.
$.get("http://remote-server.com/my_script.php" [, data ] [, success ] [, dataType ] )
You can pass variables to your server script via the data parameter. When your server returns data, you can decide how to act on it with a callback function.

Using JavaScript to send String to Servlet, and Results from servlet back to JavaScript

First of all: sorry for my bad grammer. English isn't my native language, but i will try to exlpain my problem as simple as i can.
I'm working on a web-application, where user can enter a link. (Question 1) This link should be send to the server/servlet and will be progressed to other things. (Question 2) After the progression, the servlet will send a json-array (?) back to the javascript-part of my app.
I'm completly new to this kind of stuff, but its very important to me, to find out how this works or better, how i can make this work. Its actually very simple, but i used plenty of weeks and cant figure it out.
The application is using the SAP UI5-libs (Question 3), where i would also like to know, if there is any possible way, to parse JSON with the UI5 libs.
I hope, i could explain my problem good enough, so i can get some help. Thanks to all!
The 'sending' of the string to the server/servlet would happen via ajax in either POST or GET form. That is up to you.
I recommend you use a javascript plugin like jQuery (JQuery Ajax API) because the regular ajax code is a bit messy.
As for the servlet/server communicating back to the client is as simple as writing to the page. In a typical servlet context it would be something like
out.print("This is a message");
where Ajax automatically returns the content of the entire page upon callback.
So in conclusion:
Consider test.jsp your servlet. I wish to send "Hi" from the client (being the browser) via GET to the servlet and I want the servlet to say "Hello" back.
I would open an ajax request of type GET to the url "test.jsp?param=Hi". In the servlet I receive this page request and process it. The servlet discards the parameter because it is not used and outputs "Hello" to the page.
In the client the ajax will have returned "Hello" and I can use this to put it into a var or whatever and all of this happened while not refreshing and not navigating in the original document where I did the javascript.
Another way is using websockets where you basically use sockets in javascript to send and receive any kind of data.
Also please check out this possible duplicate question: How to send a string to a servlet from javascript using xmlhttprequest

How do you make a link perform an action without reloading a page?

The clearest example of this I could think of is the Reddit Upvote/downvote buttons how when you click the button, the value for upvotes is updated, the upvote button lights up, and the page DOES NOT reload, you just stay exactly where you are on the page.
I am trying to make a feature similar to this and I can totally figure out how to do it with reloading, but I want it to not reload so the user experience isn't disrupted.
Is it possible to do this with php? or would I need to use javascript or something?
The action I would need it to perform would be a basic update query in the database.
This would be done with an Ajax call to your php script. Ajax is designed for these asynchronous updates and/or reloads.
Another way you can do this is with HTML5 WebSockets. You could have the client send a trigger to the server when the user clicks the upvote, and then the server could update and push back the data. It would, however, be a bit overfill for this.
If what you want to do is to contact a server to either send it some state or to retrieve some state from the server (or both), then you would use AJAX with javascript in order to contact the server without reloading the page. You can then also use javascript to update the state of your page after the operation. That is generally what the Reddit page you refer to is doing.
Conceptually, you'd set up your page like this:
Put the link on the page.
With javascript install an event handler so you are notified of a click on the link.
When the link is clicked, your event handler will be called.
Prevent the default behavior of the link so the browser doesn't navigate to a new page.
Then, in the event handler, send your data to the server using AJAX. You will obviously need a URL on your server and server process that can accept and process the data for you and return a value if you need to.
If you need the response from the server, then set up a callback function for when the AJAX call completes (this will be some indeterminate time in the future).
Then, if you need to change the current page in any way (like show one more upvote), then you can modify the current page with javascript to show that new state.
Ajax is easier to use with a library (like jQuery) that contains some ajax support code, but you can certainly implement it in plain javascript too.
Here's one example of ajax with plain javscript. You can find many other examples with Google.
This MDN tutorial on AJAX seems pretty helpful too to show you how it works.
You could use JavaScript to do this. Here's a quick sample:
Vote Up
Simple solution in JavaScript:
var el = document.getElementById("upvoteBtn");
el.addEventListener("click", onVoteClick);
function onVoteClick(e) {
e.preventDefault();
// do something
}
Here's a fiddle.
NOTE: I see you'd be updating the database. In that case, you would have to use AJAX in the onVoteClick function (or use XMLHttpRequest) for this. JavaScript is a client-side programming language and will not be able to communicate to the server without the use of AJAX or XMLHttpRequest. Using the jQuery library, you should be able to write AJAX pretty easy.
It's called AJAX.
With AJAX you can send a request in the background.
The easiest way is to use the jquery libary for this.
You can also output some data as JSON back to the script if you want to take some other actions depending on the result from that query.
A good tutorial is this one.
It also explains how this requests (called: XMLHttpRequest) work.
You need to use Javascript's XMLHttpRequest
You can use AJAX...
It allows you to use JavaScript (client side) to call server side functions. Here's a good example.

Categories