I've been playing around with the code (HTML2Canvas) from here: https://github.com/niklasvh/html2canvas
It's a client side Javascript tool to convert a HTML page to a canvas element.
Its uses a proxy to fetch the HTML from a remote site, it makes an Ajax call like this:
$.ajax({
data: {
xhr2:false,
url:urlParts.href
},
url: "http://html2canvas.appspot.com",
dataType: "jsonp",
success: function(html) {
This results in the following, when requesting yahoo.com as the sample URL, url being requested:
http://html2canvas.appspot.com/?callback=jQuery162020564090818326575_1311846010895&xhr2=false&url=http%3A%2F%2Fwww.yahoo.com%2F&_=1311846201150
What I want to do is roll my own JSONP proxy which I can point my copy of the code to. Trouble is I have no idea where to start.
The JSONP that is returned (I won't copy it all) begins like this:
jQuery162020564090818326575_1311846010895("<!DOCTYPE html>\n<html lang=\"en-US\" class=\"y-fp-bg y-fp-pg-grad bkt701\" style=\
So the HTML is escaped and wrapped in a callback.
I'd like to create a Python script that works along the exact same lines and generates the exact same output.
Can anyone point me in the right direction for creating a Python JSONP proxy which would generate similar output? It doesn't have to be Python, I'm just referencing that as it's what is currently used.
I put the source up for the python proxy at https://github.com/niklasvh/html2canvas-proxy. Be warned though, the script will still go through changes which will most likely break the proxy.
Related
Hi I'm new to programming and making a project exactly like these Real time GPS Tracker on JUST HTML / JS and Google Maps to be run on a handphone
My Next step is all users should see others location and one of the answers said
you'd have to send the points to a server-side script using AJAX.
I know how ajax works i just don't know what he meant by server-side script
$.ajax({
url: "sample.php",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
My Questions is:
Is the code above consider a server-side script? if no any examples
Should i make new .php file?
You have a file that exist in you server so one cannot access it using their browesers.In your case that test.html file is residing in server. it can be .php file .js file ....
The code above is not a server side scripting language as the url is still point to a .html page.
A server side scripting language is a language that runs on the server rather than on the client. example of a server side scripting language is php.
So I'm making a browser based game, and in order to create an account for this game I have the JS file call a PHP file (POST) to write an XML file.
This works, I get the file in cPanel, in the right directory, with the right content. Meaning I can open it, but only in cPanel. When I try to access it via browser I get a 404, but only for about 30 min, then it'll just magically start working.
This same PHP file is called later on in the game to update XML files, and the same thing happens. I can confirm that the PHP works exactly as it should, because I can see that the file/directory is perfect.
Here's the interesting bit, if I create an XML file manually or update it manually, it works instantly. It's only the XMLs created by the PHP file that take forever to load.
It's like the server doesn't realize that there was a change on it, until half an hour after the fact. That is, unless I did it manually.
My PHP:
<?php
$filename=$_POST['fileTo'];
$newfile=fopen($filename,"w")or die('Can not open');
$string=$_POST['stuff'];
fwrite($newfile,$string) or die('Could not write');
fclose($newfile);
?>
My AJAX call:
$.ajax({
type: 'GET',
url: writeDirect,
dataType: 'xml',
success: function(result) {
},
cache:false,
error: function(error) {
$.post('PHP/Accounts/creatAcc.php', { fileTo: userWrite, stuff: writeStuff }, function() {
signIn(userATFS, passCe);
});
}
});
Update:
I've decided to access the games directory directly from the browser. This gets even more interesting.
First thing I did was create a new account called testFile, I get the standard error on the GET because the game can't access the newly created account.
Then I opened the directory in Chrome, this is interesting:
The index clearly shows that testFile.xml exists
Then I try clicking on it, but this is where it breaks
The images 404 despite the file clearly existing
And no, changing the permissions on testFile.xml did not change anything.
I believe I've found the answer. I think it was just that server that was weird like that. I was using x10 basic and decided to switch over to another service and now it works.
I am invoking an ajax call from jquery like this:
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
container.html(data);
},
});
The data recevied from the ajax call contains script tags that reference other JS files, for ex: angularjs. Wehn I look at firebug I see that the JS files are downloaded but they do not appear in the Script tab one could debug it.
The JS files are downloaded but not executed.
How do I get around this?
The above ajax call and the container element are present in a html file called info.htm.
And edit.htm(the data fetched from the ajax call) has script tags and other html data.
Thanks.
P.S: If it helps: I can see the JS files being downloaded in the firebug 'Console' tab, however, I cant see them listed in the firebug 'Script' tab.
try with jQuery.getScript("url") for more details refer this
It makes ajax call implicitly.
or try something like :
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
$(data).appendTo(container);
},
});
I had once a problem with CSS - it had incorrect Content-Type header and was not interpreted by the browser. Can that be the problem? Does your server return text/javascript for the requested script?
May be your requirement is kind of odd because server doesn't return client code.
Best practice is load js file at the start, and you can call required method after completion of ajax script.
However, eval may not be a good practise but this is one solution.
To execute javascript code which is in variable, you need to use eval method.
Please take a look at this reference http://www.w3schools.com/jsref/jsref_eval.asp
hope this helps.
I've been trying to create a client side editor which allows the end user to create content in html or markdown. The user has two tabs for switching between the two. I managed to find some javascript that converts markdown to html, so if a user has been writing markdown and switches to the html tab, the html equivilant is shown. I haven't been able to find a javascript that converts html to markdown, only a python script.
The python script is obviously server side. The tabs are just hyperlinks with script in there. Is there any way I can convert the markdown html when the user clicks the tab?
The currently accepted answer actually tells you to do it on the server-side.
To really do client-side conversion, you could try one of these libraries (in order of popularity, measured by GitHub stars):
marked
showdown
markdown-it
markdown-js
reMarked.js
feel free to try my lib, reMarked.js, for client-side html/DOM > markdown
https://github.com/leeoniya/reMarked.js
the other way around you can try marked, but be aware that it doesn't support some php-markdown-extra features, like parsing pretty tables http://michelf.ca/projects/php-markdown/extra/#table
https://github.com/chjj/marked/
You only have to send the data to the server using AJAX, perform the conversion on the server and then return the results back to the browser. In jQuery this is as simple as e.g.:
$.ajax({
type: "GET",
url: <converter url>,
data: <html>
success: function(markdown_text){
$('#id_container').text(markdown_text);
}
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error!');
}
});
Why don't you use WMD-Editor? It has the ability to preview the html.
I have a very simple Python file, called python1.py, whose contents are:
f = open('C:\\Temp\\test.txt', 'w')
f.write('Succeeded')
f.close()
I wish to execute this from JavaScript, like so:
jQuery.ajax({
type: "POST",
url: "/cgi-bin/python1.py",
success: function (msg) {
alert("Data Saved: " + msg);
}
});
However, all that happens is that I get an alert showing me the contents of the Python script. The file C:\Temp\test.txt does not get created, so clearly the Python was not executed.
How do I persuade the code to execute the Python script instead of just reading it?
You simply need to configure your web server to execute your *.py scripts, instead of serving them as plain text.
If you are using Apache as a web server, you need to enable mod_python or mod_wsgi.
EDIT:
Since you are using using Apache, you may want to check the following article, which briefly describes how to set up the mod_python module:
A Brief Introduction to Apache's mod_python Module
You could also use the opensource project Pico. It's a really elegant way of calling server side Python code from client side Javascript.
The author has provided some simple examples here https://github.com/fergalwalsh/pico/wiki/Example-1:-Hello-World
Are you able to execute the script directly from the browser. This looks more like a webserver config issue than jquery's
If your script is that simple, you would be best off using CGI on the server side rather than mod_python or mod_wsgi as suggested by others. For details on how to set up Apache for CGI with Python and simple script examples see:
http://webpython.codepoint.net/cgi_tutorial