Is it possible to get the html produced by a script - ruby-on-rails

I have a URL from a third party site which should be the source of a script tag, something like this:
<script src="<%= #url %>"></script>
The above code shows a webform.
I would like to get the html code resulted by that code and store into a variable, something like this :
html_code = get_html('<script src="<%= #url %>"></script>')
Is that possible using Ruby/Rails (maybe using nokogiri) ?

If you're using jQuery in the browser and that code shows up in a well defined location, for example a <div> with a specific id then you can just grab it using your browser's JavaScript console:
$('#the_id').html()
That will show the raw HTML of that element presuming it dumps its content in an element with the ID the_id. You can use whatever CSS selector works.
Where that HTML goes in your document is impossible to tell from your example. A <script> can add any elements it wants anywhere in your document. You'll have to look around to see where it goes.

Load the page into a browser and then do View/Source in the browser. This will let you view the generated web page and you can find what URL was put into the <script> tag.

Related

Display desired url of webpage inline with replacement of html code like Google Translate

I want to make app which will switch vocabulary in desired url of webpage Japanese to English.
But firstable I want start form just simply display desired url of webpage inline lust like Google Translate.(See here)
I tried to use open-uri library, and get html from webpage, and display it with code below.
Controller:
def submit
require 'open-uri'
charset = nil
#html = open(params[:url]) do |f|
charset = f.charset
f.read
end
end
View:
<%= #html.html_safe %>
But I have problem with this, which simply won't load Images and some stylesheets in page.
Any way to display image and css?
If you are displaying HTML from another domain on your domain, then any relative URLs used in the HTML (links, scripts, styles, etc.) aren't going to correspond with files that exist on your domain. You are going to have to parse the HTML and either expand the URLs to full URLs that point to the original domain (easier) OR you are going to have to download all the content to your domain using the same path structure.
Example:
To display an image, a site (example.com) could use any of the following src attributes:
<img src="http://example.com/images/logo.jpg" alt="My Logo" />
<img src="/images/logo.jpg" alt="My Logo" />
<img src="../../images/logo.jpg" alt="My Logo" />
If the site always used the full URL like in the first example, you wouldn't have a problem displaying the image. If they used the second one, your site wouldn't be able to find "/images/logo.jpg" on your server. You would have to parse all the img elements and prepend the domain. The third example you would have to do even more work by determining the path to the image based on the current URL you were displaying.

Rails: Interpolation, Attribute into iFrame Link

I have an video_link attribute.
When a User creates a Listing he can enter a Code (e.g. aabbcc).
I want to render this code inside my iframe link, like so
<iframe src="https://mywebsite.com/aabbcc/"></iframe>
So i tried
<iframe src="https://mywebsite.com/#{#vine.video_link}/"></iframe>
But this didn't work,
What am i doing wrong ?
I'm Getting no Error, but if i inspect the iframe on Page, the Interpolation is not rendering. im seeing just #{#vine.video_link}
Thank you
Oh! I get it... change it to:
<iframe src="https://mywebsite.com/<%= #vine.video_link %>/"></iframe>
You only need to do interp. like that if it's inside ruby code. In this case you're not escaping a string in ruby, you're just putting it in HTML (view).
Example.
If you were in a controller, let's say and had a string that looked like
variable = "<iframe src='https://mywebsite.com/#{#vine.video_link}/'></iframe>"
Edit to correct bad copy/paste job.

tweet button inside jquery template

I want to integrate tweet button inside a jquery template. Am trying the 'tweet button with javascript' (link). But my html template shows only link, it does not show the button. I tried the anchor tag outside the jquery-templ script. Then it is working fine. I am confused why this is not working inside jquery-tmpl.
code look like follows:
<script type="text/x-jquery-tmpl">
Tweet
</script>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s) [0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="http://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
Any suggestions?
Because the closing </script> tag is not parsed correctly by the browser.
You could write </scr{{= ""}}ipt> instead...
The point is, that browsers don't understand nesting of <script> tags, so whenever it parses a </script>, it closes the outermost script tag. Therefore, the closing </script> tag is matched to the template script.
You can trick the browser into ignoring the script tag, by adding the {{= ""}}, which will be used by the template engine to generate an empty string. </scr{{= ""}}ipt> will result in </script>, after templating.

Embedded HTML code being displayed rather than HTML being rendered

I am trying to use the calendar gem in my project (https://github.com/elevation/event_calendar). But when I open the calendar page, it renders the page by showing the html code of the calendar rather than rendering the html. Basically the source for the page generate is like
<div class="ec-calendar">
instead of
.
Can anyone let me know what is going on and how to resolve it.
I assume you are using Rails 3? As a security measure against XSS (Cross Site Scripting), Rails 3 renders html inside of strings as text. If you know the html in your string is safe, call html_safe on it, like
'<div class="ec-calendar">'.html_safe
or
raw '<div class="ec-calendar">'
html_safe I believe, is preferred over raw. Not sure what's different behind the scenes, if anything.

how to parse jquery ajax xhtml response?

Sorry if this has been posted many times. But I've tried many variations and it still doesn't work. The HTML comes back from the jquery AJAX call fine and I am trying to remove the header and footers from the response using:
// none of these work for me
$("#content", data);
$("#content", $(data));
$(data).find("#content").html()
I've breakpoint the response to verify the #content exists by inspected $(data) and using alert to print out the data's text. I've also try using "body" or "a" as selectors, but it always come back as undefined.
I've read in this post that you can't pull in the full XHTML document: jquery ajax parse response text. But I can't find the answer's quote anymore, maybe it's outdated?
Has anyone ran into this problem?
Many thanks,
Steve
this works for me:
$(data).filter("#content");
You need a div to attach your data to. Like $("#response").replaceWith($(data).find('#content'));
That should work
It works when there is a <div> tag in the received document.
E.g. <body><div> your content </div></body>.
See a very simple proof of concept.
The data you receive from your AJAX call is not a part of your DOM tree, so you can not use JQuery function calls to manipulate it. You can use text manipulation functions, you can use JSON, or you may also attach your response to your DOM.
Are you using the get-method? Alternatively you could use the jQuery load method where you can provide a page fragment to load.
For example to load a content div from a wellformed html document you could use
$("#div-to-load-to").load("html-doc-to-load-from.html #content", function() {
//do something
});
You MUST guarantee that your HTML dom was well formatted.
Try the simplest HTML
<html>
<head>
<title>title here</title>
</head>
<body>
<div>
<div>hello</div>
<span id="s">span content</span>
</div>
</body>

Resources