Javascript: passing a URL to google encoding trouble - javascript

I'm trying to look at the html data returned from a google reverse image search. The URL I'm passing to google contains some characters that get encoded, notably ? changes to %3F, and google doesn't seem to understand the formatted URL (pic_url below). Is there any way to send the URL so that it does not get formatted? Or is there another way around this issue?
My code looks like:
var google_url = "https://www.google.com/searchbyimage?image_url=";
var pic_url = "http://img.cpcdn.com/recipes/_o1f2b886e/50x50c/d9e62798f1c807c1891454bed562e4c9.jpg?u=941483&p=1362455199";
var search_url = google_url + pic_url;
$.getJSON('http://whateverorigin.org/get?url=' +
encodeURIComponent(search_url) + '&callback=?',
function(data){
$("#targetWrapper").html(data.contents);
http_data = data["contents"];
console.log(http_data);
});
The error I get back in the console:
`The requested URL <code>/searchbyimage%3Fimage_url=http%253A%252F%252Fimg.cpcdn.com%252Frecipes%252F_o1f2b886e%252F50x50c%252Fd9e62798f1c807c1891454bed562e4c9.jpg%253Fu%253D941483%2526amp%253Bp%253D1362455199</code>
was not found on this server.
<ins>That’s all we know.</ins>`
But if you just copy and paste https://www.google.com/searchbyimage?image_url=http://img.cpcdn.com/recipes/_o1f2b886e/50x50c/d9e62798f1c807c1891454bed562e4c9.jpg?u=941483&p=1362455199 to the Address Bar it works. Any thoughts?

there is a bug in whateverorigin.org:
https://github.com/ripper234/Whatever-Origin/issues/1
note that the solution proposed doesn't work for me...

Related

Uncaught URIError: URI malformed with reCAPTCHA

On some pages that have an encoded part in the url, like %FC instead of ü, I get a Uncaught URIError: URI malformed error in the console and the captcha therefore is not working.
Our Project is iso-8859-1 encoded and there is nothing I can change about that.
Do you know a workaround that could fix this? I'm using reCAPTCHA 2.
My solution now is, that before the recaptcha/api.js is executed, I grab the window.location.pathname, get rid of the problematic elements and exchange it in the url with history.pushState. If I'd simply changed the window.location.pathname a page redirect would be the consequence.
Here is my example code:
var oldPath = window.location.pathname;
var newPath = decodeURIComponent( unescape( unescape(oldPath)));
var stateObj = {};
history.pushState(stateObj, "", newPath);

How to get number of video views with YouTube API into a Google Spreadsheet?

Anyone figured out a way to get number of video views with YouTube API into Google Docs?
Up until recently I was successfully using the method outlined in the YouTube View Count in Google Spreadsheet blog post by David Toy.
But it stopped working recently and I began to get the below error:
Request failed for https://gdata.youtube.com/feeds/api/videos/Prv2-9U39K8?v=2&alt=json returned code 410.
Truncated server response: <errors xmlns='http://schemas.google.com/g/2005'>
<error><domain>GData</domain><code>NoLongerAvailableException</code>
<internalReason>No longer avai...
(use muteHttpExceptions option to examine full response) (line 2, file "Code")
Has anyone run into this? If so any ideas on how to fix it would be really helpful.
Thanks!
The script from that blog post uses v2 of the API which has now been deprecated and switched off. The new version would be:
function getYoutubeViews(videoId){
var url = "https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" + videoId;
url = url + "&key={YOUR-API-KEY}";
var videoListResponse = UrlFetchApp.fetch(url);
var json = JSON.parse(videoListResponse.getContentText());
return json["items"][0]["statistics"]["viewCount"];
}
In order to make this work you will need an API key. Here is the guide on the YouTube developers site which explains how to get the key. Once you have obtained your key simply replace {YOUR-API-KEY} in the code above with it.
To call the function from a Google spreadsheet, select a cell and enter: =getYoutubeViews("{YOUR-VIDEO-ID}"). For instance, =getYoutubeViews("IiPJsI8pl8Q").

How to escape absolute URI?

In the script: `http://theip.com/something/index.php
I have the following javascript URI:
var uri = '/something/script.php?=' + someDynamicValue
That I pass to a function "loadHTML(url, div)"
someDynamicValue can contain spaces and other symbols which make JQuery crash with $.load().
So, I try to encode uri:
$('#'+div).load(encodeURIComponent(uri));
And gives
http://theip.com/something/%2Fsomething%2Fscript.php%3Fq%3D?_=1399924421585
That is, duplicating the /something (which should be an absolute URL so it should go to http://ip.com/something/script.php)
Now if I do the following:
$('#'+div).load(encodeURIComponent(uri).replace(/%2F/g,'/'));
I get a "good" url but gives 404 Error:
http://theip.com/something/script.php%3Fq%3D?_=1399923477529
So I guess it is taking script.php%3Fq%3D?_=1399923477529 as a literal script name, maybe.
How can I fix it? (Encode the rest of the URL).
Thanks!
You just need to encode the one part that isn't already properly URI encoded:
var uri = '/something/script.php?foo=' + encodeURIComponent(someDynamicValue)
$('#'+div).load(uri);

javascript window.location gives me a wrong url path when checking firebug

I have a sample url website: http://mysite.com/
var host = window.location.protocol+"//"+window.location.hostname;
$.ajax({
type:"POST",
data: params,
url : host+'/forms/get_data.php',
success:function(data){
...othercodeblahblah
}
});
Why is it that when I try to check my firebug it makes the URL weird.
This is the sample output of firebug:
http://mysite.com/mysite.com/forms/get_data.php
With this url it now gives me:
"NetworkError: 404 Not Found - http://mysite.com/mysite.com/forms/get_data.php"
Shouldn't it output like http://mysite.com/forms/get_data.php ?
Why is it giving me a wrong url path?
Your help would be greatly appreciated and rewarded!
Thank!
The reason is window.location.protocol already includes a colon (:).
The host variable therefor contains http:://mysite.com
jQuery picks up that you didn't pass a full valid URL, so it prepends your hostname automatically.
The fix is changing
var host = window.location.protocol+"://"+window.location.hostname;
to
var host = window.location.protocol+"//"+window.location.hostname;
Edit
I created a jsfiddle with your code: http://jsfiddle.net/xH5ZV/
and the fixed code: http://jsfiddle.net/xH5ZV/1/
Notice in the fixed code you don't get the hostname twice.
I'm not sure where such an error might come from, but specifying the host is redundant: AJAX requests are same-domain anyway (unless specifically configured), so just specify a part from the root:
url: "/forms/get_data.php",

Get link to render as html rather than text json

I am responding to clicks on li's by using $.post to post to an action method in my MVC application.
I want to send a link back in Json.
Can I have this link render as html rather than text ? how ?
I tried this, just to test the html:
var link = "<b>Hi</b>";
var encoded = Server.HtmlEncode(link);
that came out as <b>Hi</b>
Surely there is just a Json.encode or visual studio method I can use and I don't have to format it myself? Have googled fairly extensively and can't find anything about an Json.encode
var link = "<b>Hi</b>";
var encoded = new JavaScriptSerializer().Serialize(link);
the page rendered "\u003cb\u003eHi\u003c/b\u003e"
If I send just the link variable, i.e:
var link = "<b>Hi</b>"
<b>Hi</b> renders
This is the line which sends it back:
return Json(new {Title = pTitle, Selection = pSelection, Link = pLink}, JsonRequestBehavior.AllowGet);
Starting to get frustrated, wtf!
Silly me, I didn't post enough code where the problem was:
<script type="text/javascript">
function TreeView_onSelect(e) {
...
$.post(url, id, function (data, textStatus) {
...
$("#panel-link").text(data.Link);
}
$("#panel-link").text(data.Link);
obv has to be
$("#panel-link").html(data.Link);
Try using JavaScriptSerializer:
var link = "<b>Hi</b>";
var encoded = new JavaScriptSerializer().Serialize(link);
Try to use javascript's decodeURI() function.
http://www.w3schools.com/jsref/jsref_decodeuri.asp
<script type="text/javascript">
var uri="mytest.asp?name=ståle&car=saab";
document.write(encodeURI(uri)+ "<br />");
document.write(decodeURI(uri));
</script>
The output of the code above will be:
mytest.asp?name=st%C3%A5le&car=saab
mytest.asp?name=ståle&car=saab
I have the same problem with you and killing me whole day,
I solved this problem by using Json.NET
Sample code is :
Newtonsoft.Json.JsonConvert.SerializeObject(link);
Reference
http://json.codeplex.com/documentation

Categories