Copy to clipboard script with breaks and more text - java

I am trying to make a fun script for work documentation. Here is what I have so far.
<script type="text/javascript">
function ClipBoard() {
window.clipboardData.setData('text',
document.getElementById('name').value +
document.getElementById('phone').value +
document.getElementById('serial').value +
document.getElementById('new').value +
document.getElementById('cuts').value +
document.getElementById('agts').value
);
}
</script>
<form id="form1">
Name: <input id="name" /><br />
Phone Number: <input id="phone" maxlength="10" /><br />
Serial Number: <input id="serial" maxlength="10" /><br />
New/Existing: <input id="new" /><br />
CU TS: <input id="cuts" /><br />
Agent TS: <input id="agts" /><br />
<input type="button" onclick="ClipBoard()" value="Copy"/>
<input type="reset" />`
Right now after I paste the inputs do not have "breaks" instead, the copied text copies in a line. Ex: namephoneserialnew etc.
I would like:
Name
Phone
Serial
New
Etc. with breaks.
If at all possible.
Also, when copying the inputs, is there a way to copy the text before the input.
Ex: Name: (with input), Phone Number: (with input) etc.
Any suggestions will be very helpful; this is just a basic script nothing serious. Thanks everybody!

Try to add '\r\n' after ctl value.
function ClipBoard() {
window.clipboardData.setData('text',
document.getElementById('name').value + '\r\n' +
document.getElementById('phone').value + '\r\n' +
document.getElementById('serial').value + '\r\n' +
document.getElementById('new').value + '\r\n' +
document.getElementById('cuts').value + '\r\n' +
document.getElementById('agts').value
);
}

JavaScript doesn't generate line breaks this way. You may try adding "<br>" in the code and that might bring up the line breaks.
function ClipBoard() {
window.clipboardData.setData('text',
document.getElementById('name').value + "<br>"
document.getElementById('phone').value + "<br>"
document.getElementById('serial').value + "<br>"
document.getElementById('new').value + "<br>"
document.getElementById('cuts').value + "<br>"
document.getElementById('agts').value
);
}
And if you want Labels before the values you can just put a string before them.
function ClipBoard() {
window.clipboardData.setData('text',
"Name: " + document.getElementById('name').value + "<br>"
"Phone: " + document.getElementById('phone').value + "<br>"
"Serial: " + document.getElementById('serial').value + "<br>"
"New: " + document.getElementById('new').value + "<br>"
"Cuts: " + document.getElementById('cuts').value + "<br>"
"Agts: " + document.getElementById('agts').value
);
}
BTW, you forgot the ending tag in the HTML.

Related

JSOUP - Select only some text from html

I am trying to select some text from the HTML using Jsoup in Android.
My HTML code looks like that:
<tr class="tip " data-original-title="">
<td>
!!! NOT That !!! </td>
<td>
A205 </td>
<td>
I want to get this </td>
<td>
And this </td>
<td>
!!! And not this !!! </td>
<td>
</td>
</tr>
How can I do that? Thank you so much!
For example:
package ru.java.study;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Main {
private static String htmlText =
"<tr class=\"tip \" data-original-title=\"\">" +
"<td>!!! NOT That !!!</td>" +
" <td>" +
" A205 </td>" +
" <td>" +
" I want to get this </td>" +
" <td>" +
" And this </td>" +
" <td>" +
" !!! And not this !!! </td>" +
" <td>" +
" </td>" +
" </tr>";
public static void main(String[] args) {
Document document = Jsoup.parse("<table>"+htmlText); //Add <table>
String first_TD = document.select("td").get(2).text();
String second_TD = document.select("td").get(3).text();;
System.out.println(first_TD);
System.out.println(second_TD);
}
}
You must be more specific in your selection. There should be id="..." or class="..." attributes in <table> tag to precisely identify the table that you need.
// Don't forget about <table> tag
String html = "<table>" +
"<tr class=\"tip \" data-original-title=\"\">" +
"<td>!!! NOT That !!!</td>" +
"<td>A205</td>" +
"<td>I want to get this</td>" +
"<td>And this</td>" +
"<td>!!! And not this !!!</td>" +
"<td></td>" +
"</tr>" +
"</table>";
Document doc = Jsoup.parseBodyFragment(html);
// You should use more specific selector.
// For example if table tag looks like this: <table id="myID">...</table>
// then selector should look like this "table#myID tr.tip > td"
Elements cells = doc.select("tr.tip > td");
String cell3content = cells.get(2).html(); // use .text() for content without html tags
String cell4content = cells.get(3).html();
System.out.println(cell3content);
System.out.println(cell4content);

How to extract specific text in html code with JSoup

I have a website where I want to extract some data from. I want to extract the 8a on the second line (a-element) with JSoup. I can not use Regex because sometimes 8a is just 2 or 7c+ and these same values can be in the text in between the a tags as well. Ideas?
<div class="vsr">
L'Américain (intégral) 8a
<span class="ag">7c+</span>
<em>Tony Fouchereau</em>
<span class="btype">traversée d-g, surplomb, départ assis</span>
<span class="glyphicon glyphicon-camera" aria-hidden="true"></span>
<span class="glyphicon glyphicon-film" aria-hidden="true"></span>
</div>
You can use Jsoup css selectors to extract specific information.
https://jsoup.org/cookbook/extracting-data/selector-syntax
#Test
public void extract8a() {
Document doc = Jsoup.parse("<div class=\"vsr\"> \n" +
" L'Américain (intégral) 8a \n" +
" <span class=\"ag\">7c+</span> \n" +
" <em>Tony Fouchereau</em> \n" +
" <span class=\"btype\">traversée d-g, surplomb, départ assis</span> \n" +
" <span class=\"glyphicon glyphicon-camera\" aria-hidden=\"true\"></span> \n" +
" <span class=\"glyphicon glyphicon-film\" aria-hidden=\"true\"></span> \n" +
"</div>");
System.out.println(doc.select("div.vsr").first().ownText());
}

Extract data from HTML page in between Tags using HTMLUNIT

I'm trying to extract data from web page using Html Unit. I've already achieved this by converting HtmlPage to text and then extracted data by using regular expression out of that HTML page. I've also achieved to extract data from Html tables using class attribute in Html.
I want to use HtmlUnit again fully for all extraction to learn for the same requirement I have done using regular expression. Am not able to get how can I extract data within tags in the form of key value pair.
Here is the sample Html data
<div class="top_red_bar">
<div id="site-breadcrumbs">
Home
|
Queues
|
Topics
|
Subscribers
|
Connections
|
Network
|
Scheduled
|
<a href="/admin/send.jsp"
title="Send">Send</a>
</div>
<div id="site-quicklinks"><P>
<a href="http://activemq.apache.org/support.html"
title="Get help and support using Apache ActiveMQ">Support</a></p>
</div>
</div>
<table border="0">
<tbody>
<tr>
<td valign="top" width="100%" style="overflow:hidden;">
<div class="body-content">
<h2>Welcome!</h2>
<p>
Welcome to the Apache ActiveMQ Console of <b>localhost</b> (ID:TOOLCONTROLPJX526-524666-65544585445-2:3)
</p>
<p>
You can find more information about Apache ActiveMQ on the Apache ActiveMQ Site
</p>
<h2>Broker</h2>
<table>
<tr>
<td>Name</td>
<td><b>localhost</b></td>
</tr>
<tr>
<td>Version</td>
<td><b>5.13.3</b></td>
</tr>
<tr>
<td>ID</td>
<td><b>ID:TOOLCONTROLPJX526-524666-65544585445-2:3</b></td>
</tr>
<tr>
<td>Uptime</td>
<td><b>17 days 13 hours</b></td>
</tr>
<tr>
<td>Store percent used</td>
<td><b>19</b></td>
</tr>
<tr>
<td>Memory percent used</td>
<td><b>0</b></td>
</tr>
<tr>
<td>Temp percent used</td>
<td><b>0</b></td>
</tr>
</table>
I want to extract data in between table tag.
Expected output
Name:localhost
Version:5.13.3
ID:ID:TOOLCONTROLPJX526-524666-65544585445-2:3
Uptime:7 days 13 hours
Store percent used:19
Memory percent used:0
Temp percent used:0
How it can be achieved? I want to know which methods to be used within HTLM unit to achieve this.
This are the steps i followed (not the only solution)
parse the string through parseHtml method with dummy url
get the second table by xpath
iterate with double nested loop (for and iterator -to append separator correctly-)
ExtractTableData:
import java.net.URL;
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HTMLParser;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow.CellIterator;
public class ExtractTableData {
public static void main(String[] args) throws Exception {
String html = "<div class=\"top_red_bar\">\n" + " <div id=\"site-breadcrumbs\">\n"
+ " Home\n"
+ " |\n"
+ " Queues\n"
+ " |\n"
+ " Topics\n"
+ " |\n"
+ " Subscribers\n"
+ " |\n"
+ " Connections\n"
+ " |\n"
+ " Network\n"
+ " |\n"
+ " Scheduled\n"
+ " |\n" + " <a href=\"/admin/send.jsp\"\n"
+ " title=\"Send\">Send</a>\n" + " </div>\n"
+ " <div id=\"site-quicklinks\"><P>\n"
+ " <a href=\"http://activemq.apache.org/support.html\"\n"
+ " title=\"Get help and support using Apache ActiveMQ\">Support</a></p>\n"
+ " </div>\n" + " </div>\n" + "\n"
+ " <table border=\"0\">\n" + " <tbody>\n"
+ " <tr>\n"
+ " <td valign=\"top\" width=\"100%\" style=\"overflow:hidden;\">\n"
+ " <div class=\"body-content\">\n" + "\n" + "\n"
+ "<h2>Welcome!</h2>\n" + "\n" + "<p>\n"
+ "Welcome to the Apache ActiveMQ Console of <b>localhost</b> (ID:TOOLCONTROLPJX526-524666-65544585445-2:3)\n"
+ "</p>\n" + "\n" + "<p>\n"
+ "You can find more information about Apache ActiveMQ on the Apache ActiveMQ Site\n"
+ "</p>\n" + "\n" + "<h2>Broker</h2>\n" + "\n" + "\n" + "<table>\n" + " <tr>\n"
+ " <td>Name</td>\n" + " <td><b>localhost</b></td>\n" + " </tr>\n" + " <tr>\n"
+ " <td>Version</td>\n" + " <td><b>5.13.3</b></td>\n" + " </tr>\n" + " <tr>\n"
+ " <td>ID</td>\n" + " <td><b>ID:TOOLCONTROLPJX526-524666-65544585445-2:3</b></td>\n"
+ " </tr>\n" + " <tr>\n" + " <td>Uptime</td>\n"
+ " <td><b>17 days 13 hours</b></td>\n" + " </tr>\n" + " <tr>\n"
+ " <td>Store percent used</td>\n" + " <td><b>19</b></td>\n" + " </tr>\n"
+ " <tr>\n" + " <td>Memory percent used</td>\n" + " <td><b>0</b></td>\n"
+ " </tr>\n" + " <tr>\n" + " <td>Temp percent used</td>\n" + " <td><b>0</b></td>\n"
+ " </tr>\n" + "</table>";
WebClient webClient = new WebClient();
HtmlPage page = HTMLParser.parseHtml(new StringWebResponse(html, new URL("http://dummy.url.for.parsing.com/")),
webClient.getCurrentWindow());
final HtmlTable table = (HtmlTable) page.getByXPath("//table").get(1);
for (final HtmlTableRow row : table.getRows()) {
CellIterator cellIterator = row.getCellIterator();
if (cellIterator.hasNext()) {
System.out.print(cellIterator.next().asText());
while (cellIterator.hasNext()) {
System.out.print(":" + cellIterator.next().asText());
}
}
System.out.println();
}
}
}
Output:
Name:localhost
Version:5.13.3
ID:ID:TOOLCONTROLPJX526-524666-65544585445-2:3
Uptime:17 days 13 hours
Store percent used:19
Memory percent used:0
Temp percent used:0

spring email buttons not working

I want to send a email message, Will have 1 link to load image from a url.
Then 2 buttons. one button to re direct to google.com and one link to redirect cnn.com
#Autowired
public JavaMailSender emailSender;
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("ratfat#gmail.com");
helper.setSubject("Test subject");
String googleLink="http://google.com";
String cnnLink="http://cnn.com";
String emailContent="\r\n" +
"<html>\n" +
" <body> \n" +
" <div align='center'>\n" +
" <div style='text-align:center;'>\n" +
" <IMG src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png' height='75' width='150'>\n" +
" </div>\n" +
" \n" +
" <br/><br/>\n" +
" Would you like to take it?<br/><br/><br/> \n" +
" <br/> \n" +
" <input type=\"button\" id=\"acceptAcct\" onclick=\"location.href='"+googleLink+"';\" style=\"width:10em;height:5em\" value=\"Click to load google\" />    \n" +
" <input type=\"button\" id=\"rejectAcct\" onclick=\"location.href='"+cnnLink+"';\" style=\"width:10em;height:5em\" value=\"Click to load CNN\" /> \n" +
" </div>\n" +
" \n" +
" </body>\n" +
" </html>\n";
helper.setText(emailContent, true);
message.setContent(message, "text/html");
emailSender.send(message);
I copy the html printed out to a simple test.html and it works well in my browse.
But when the email is recieved, i try clicking the button nothing happens.
In the email ,when i use chrome debug to see the code, the input buttons are like below.
<input type="submit" id="m_-5702442034034898245acceptAcct" style="width:10em;height:5em" value="Click to load google">
<input type="submit" id="m_-5702442034034898245rejectAcct" style="width:10em;height:5em" value="Click to load cnn">
1). The id is differnt, i dont know where the numbers came from.
2.) The onclick=\"location.href='"+googleLink+"';\" are not there as part of input button.
There are significant limits on the type of html you can use in an email message, and every mail client will impose different limits. Generally, these limits aren't documented, but this might help you get started.
try this:"<a href= '"+googleLink+"' > \n"+ " <input type=\"button\"style=\"width:10em;height:5em\" value=\"Click me to accept\" /> \n" + "</a>\n"+

Invoking Java Applet with dynamic parameters

Here's the scenario. The user fill a form with a certain number of parameters in an html page, then I need to invoke my applet with this specific parameters, possibly within the same page. What's the easiest way to perform this pass?
I'm using the runApplet function, but it's not working. No messages in the JS console and in the Java console.
<html>
<head>
<title>Compila i dati</title>
</head>
<body>
<script src=
"http://www.java.com/js/deployJava.js"></script>
<script>
function runApplet(){
var attributes = { id:'anID', code:'Test', width:1, height:1, codebase: '.'} ;
var parameters = {width:'100', height:'100', code:'Test', archive: 'applet.jar, xyz.jar, abc.jar',
posX: document.forms["form1"]["posX"].value , posY: document.forms["form1"]["posY"].value , heightSign: '300' , widthSign: '600' ,
PDFUrl: 'http://anurl' ,
type:'application/x-java-applet' , scriptable:'false' } ;
deployJava.runApplet(attributes, parameters, 1.6);
}
</script>
<form name = "form1" onsubmit="runApplet()">
<div>
<label for="name">Nome:</label>
<input type="text" name="nome" />
</div>
<div>
<label for="mail">Cognome:</label>
<input type="text" name="cognome" />
</div>
<div>
<label for="msg">X:</label>
<input type="text" name="posX" />
</div>
<div>
<label for="msg">Y:</label>
<input type="text" name="posY" />
</div>
<div class="button">
<button type="submit">Invia</button>
</div>
</form>
</body>
I could try to create an dinamic applet contanier via javascript in this way:
$('#idappletv').empty();
$('#idappletv').hide();
var _xmhlcode = " <script> function javafxEmbed() {" +
"dtjava.embed( " +
"{" +
" id: 'myBrApplet'," +
" url : 'pages/applet/myApplet.jnlp'," +
" placeholder : 'javafx-app-placeholder'," +
" width : 890 ," +
" height : 200," +
" jnlp_content : 'bmFtZT0iQXBwbGV0RnhCcm93c2VyIiAvPg0KICA8dXBkYXRlIGNoZWNrPSJhbHdheXMiLz4NCjwvam5scD4NC=='" +
", params: {param1:'" + param1 + "',param2:'" + param2 + "'}" +
"}," +
"{" +
" javafx : '2.2+'" +
"}," +
"{}" +
");" +
"}" +
"dtjava.addOnloadCallback(javafxEmbed); </" + "script> ";
$('#idappletv').append("<div id='javafx-app-placeholder'></div>");
$('#idappletv').append(_xmhlcode);
dtjava.addOnloadCallback(javafxEmbed);
$('#idappletv').show();
Then when the applet starts you can read the parameter using:
#Override
public void start(Stage primaryStage) {
Parameters params = getParameters();
String param1 = params.getNamed().get("param1");
String param2 = params.getNamed().get("param2");
You can read the parameter "param1 and param2" from your html page.
It should be works.
I used JavaFX with java-webstart (http://docs.oracle.com/javafx/2/deployment/deploy_swing_apps.htm).

Categories

Resources