Ruby Mechanize gem not following meta refresh - ruby

I have a Ruby 2.2 automation script that uses Mechanize to log in to Google Payments. When I try to access the url Mechanize stops on the meta refresh. The content of the page is:
<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<script type="text/javascript" language="javascript">
var url = 'https:\/\/accounts.google.com\/ServiceLogin?
service\x3dbilling\x26passive\x3d1209600\x26continue\x3dhttps:\/\/payments.google.com\/
payments\/home%23__HASH__\x26followup\x3dhttps:\/\/payments.google.com\/payments\/
home'; var fragment = ''; if (self.document.location.hash) {fragment = self.document.
location.hash.replace(/^#/,'');}url = url.replace(new RegExp("__HASH__", 'g'),
encodeURIComponent(fragment));window.location.assign(url);
</script><noscript><meta
http-equiv="refresh" content="0; url='https://accounts.google.com/ServiceLogin?
service=billing&passive=1209600&continue=https://payments.google.com
/payments/home&followup=https://payments.google.com/payments/home'"></meta>
</noscript></head>
<body></body>
</html>
Here is the part of my script to get to the login screen:
#agent = Mechanize.new
#agent.follow_meta_refresh = true
page = #agent.get("http://payments.google.com/payments/home")
puts page.content
The page.content at the end only shows the above html, the meta refresh is not followed. Any suggestions on how I can follow that would be greatly appreciated.

Assuming the script isn't really reformatted like that:
url = page.body[/url = '(.*?)'/, 1]
page = #agent.get url

Related

Cannot print this document yet, it is still being loaded - Firefox Printer Error

My API generates dynamic HTML document and dumps it into a popup window like so:
var popup = window.open('', "_blank", 'toolbar=0,location=0,menubar=1,scrollbars=1');
popup.document.write(result);
After the document is reviewed by a user, they can print it calling
window.print();
Chrome handles it without any problems, but Firefox shows a Printer error:
"Cannot print this document yet, it is still being loaded"
Printer window opens only if I hit Ctrl+R.
It appears that $(document).ready() never happens in firefox and it keeps waiting for something to load.
Status bar in popup says Read fonts.gstatic.com
Here's a brief content of a document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://fonts.googleapis.com/css?family=Orbitron|Jura|Prompt" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<title>Invoice #15001</title>
<style>
...
</style>
</head>
<body>
<div id="invoice_body" >
...
</div><!-- Invoice body -->
</body>
</html>
I have a feeling it has something to do with Google fonts. Any help is appreciated
When you pass "" as the URL to window.open, Firefox loads 'about:blank' at which point script security is likely preventing you from pulling in external resources via http or https ...
I am able to reproduce your problem and have it popup with the same error when I try to print-- I was able to get it working by using a data url when calling window.open ...
Based on your example, result is a string containing the HTML for the popup, so you would call window.open like this, and no longer use document.write for anything:
var popup = window.open("data:text/html;charset=utf-8,"+result, "printPopup", "toolbar=0,location=0,menubar=0,scrollbars=1");
I tested this with result being a string containing:
<html><head>
<link rel="stylesheet"href="https://fonts.googleapis.com/css?family=Tangerine">
<style> body { font-family: 'Tangerine', serif; font-size: 48px; } </style>
<title>Test</title></head>
<body>
<div>Testing testing</div>
<div>Print</div>
</body>
</html>
And clicking the print link worked as expected...
I had to go an extra mile, but:
I added server side code that would save a html file and pass a link to that file instead of html content:
ob_start();
include('ezts_invoice_template.php');
$dom = ob_get_clean();
$ezts_file_path = EZTS_PLUGIN_PATH.'kernel/tmp/'.session_id().'_tmp.html';
$ezts_file = fopen($ezts_file_path, 'w+');
$result = fwrite($ezts_file, $dom);
fclose($ezts_file);
print_r('{"result":"success", "file":"'.plugin_dir_url(__FILE__).'tmp/'.session_id().'_tmp.html"}');
in JS I open a popup by a link passed from PHP:
var popup = window.open(result.file, "_blank", 'toolbar=0,location=0,menubar=0,scrollbars=1');
and, finally, in template file I added event listener to request deletion of temporary file when the window is closed
window.addEventListener('beforeunload', function(event) {
window.opener.eztsApiRequest('deleteTempFile',
'',
function(result, status){ console.log(result); });
}, false);
It's not as easy, but it works great.

Cannot Load the HTML through HtmlAgilityPack

I try to parse HTML using HtmlAgilityPack using simple doc.load method by passing the URL, but it comes with the following result how can I resolve this issue?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";
expires=" + exdate.toGMTString()) + ";path=/"; }
function getHostUri()
{ var loc = document.location; return loc.toString(); }
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '202.142.170.42', 10);
setCookie('DOAReferrer', document.referrer, 10); location.href = getHostUri();
</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript>
</body></html>
This site requires JavaScript and Cookies to be enabled.
Please change your browser settings or upgrade your
browser.
This Message says it all, the side needs javascript to be loaded, and HtmlAgilityPack is no JavascriptEngine!
The Load Method of the HtmlDocument can not interpret and execute Javascript-Code it´s just a simple "Download"-Function for static HTML-Sites.
What you could try to do is, with Firebug (or something else) check which HttpRequest are made to get the content, and this Requests you have to recreate in C# to get the HTML you want!
Here are some similar Questions:
Running Scripts in HtmlAgilityPack
C# - Get JavaScript variable value using HTMLAgilityPack
Calling javascript function from HtmlAgilityPack

Nokogiri query for HTML comments contained in JavaScript?

I have the following HTML and am trying to get the comments in the script nodes:
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
url = 'http://someurl.com';
-->
</script>
</head>
</html>
Using this, I get the script nodes:
javascript_code = doc.xpath("/html/head/script")
But, when adding comments() to xpath, it returns nothing:
javascript_code = doc.xpath("/html/head/script/comment()")
I have no idea why this is not working, it seems like it should be simple. Is it possible to get the comment?
If you parse the document as XML, it will find the comment. However, if you parse it as HTML, Nokogiri will put the entire contents of the script tag into a cdata section. You could then parse it out.
require 'rubygems'
require 'nokogiri'
body = DATA.read
doc = Nokogiri::XML(body)
puts doc.search('/html/head/script/comment()').text.strip
# puts "url = 'http://someurl.com';"
doc = Nokogiri::HTML(body)
puts doc.search('/html/head/script').text.strip
# puts "<!--\n url = 'http://someurl.com';\n -->"
__END__
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
url = 'http://someurl.com';
-->
</script>
</head>
</html>

sending an url but staying on the same page ( php, codeigniter, javascript )

this is my situation. I'm writing an webinterface in codeigniter to send commands to a virtual server. these commands are passed through the browser in an url.
example : http://server.com/TgtSendIRCommand?id=UM-02&code=KEY_UP
this server then takes action and the web interface refreshes the screenshot.
i just want to have a bunch of links in html that send this command to the browser.
But if i click the links, the user has to stay on the same page. So the command is sended but there is no other interaction.
i am using codeigniter.
Any suggestions ?
Make a view and copy this in your view:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
</head>
<body>
Click here!
<script type="text/javascript">
$(document).ready(function(){
$("#myBtn").click(function(){
$.get("http://server.com/TgtSendIRCommand?id=UM-02&code=KEY_UP ");
});
});
</script>
</body>
</html>
Not tested but something as above should do the trick for you.
<?php
session_start();
$_SESSION['http://localhost/profile.php'] = $_SERVER['REQUEST_URI'];
$sess=$_SESSION["email"];
$servername = "localhost";
$username = "root";
$password = "";
$dbname ="reg";
if(!isset ($sess))
{
header("Location:login.php");
exit;
}

Redirect after few second in rails 3.1

Is there a way to redirect after few second only by using rails?
I want when A user click on a link see the page and after few second get redirected to his profile(user_path)
Because HTML is part of a Rails application you could use the standard html redirect option by default:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/index.html">
Change 0 to number of seconds you want to wait before redirection.
On the other hand you could use javascript (also part of rails applications):
<html>
<head>
<script type="text/javascript">
function delayedRedirect(){
window.location = "/default.aspx"
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html>
You can use sleep method
sleep(2.0)
redirect_to root_path

Resources