Select SOME text from a page and use it as a var

M

melvynadam

If I open a new page with very little text on it such as
a bugmenot
results page
is there a way to select some of the text there and
assign it to variables?
Selecting all and copying to the clipboard can be done with:
document.execCommand("SelectAll");
document.execCommand("Copy");
but I'm looking for something a little more refined.

The page contains seven lines of text and I'd like a simple way to say:
var username = [contents of line 4]
var password = [contents of line 5]
Can anyone point me in the right direction?
 
Z

ZER0

If I open a new page with very little text on it such as
a bugmenot
results page
is there a way to select some of the text there and
assign it to variables?

on the fly:

function getCurrentSelection(){
if (window.getSelection)
return typeof window.getSelection().toString();
else if (document.getSelection)
return document.getSelection()
else if (document.selection && document.selection.createRange)
return document.selection.createRange().text;
else
return null;
}

var sel=getCurrentSelection();

maybe it could be help you.

--
ZER0

~ The Tangent Universe collapsed 5891 days, 9 hours, 2 minutes and 6 seconds ago.

on air ~ "(Unknown Artist) - The Artifact And Living"
 
M

melvynadam

Thanks for the incredibly quick response! Unfortunately it's not quite
what I'm looking for - I want the process to be automated. If I have to
manually select the text it negates the point of the plugin I'm trying
to create.
I wish to open a bugmenot results page using my current location (e.g.
www.nytimes.com) with this line:
window.open('http://www.bugmenot.com/view.php?mo...=yes,menubars=no,toolbars=no,resizable=yes');
Then, in the new window I wish to automatically select the only text on
that page which changes (take a look at
http://www.bugmenot.com/view.php?mode=bookmarklet&url=http://www.nytimes.com
if you copy and paste all of the text you'll see that it's nicely
formatted and the two desired fields are on their own on individual
lines).
Later I'll want to try to return to the original page and find the
fields "user" or "username" etc. and paste back my results.
 
Z

ZER0

I wish to open a bugmenot results page using my current location (e.g.
www.nytimes.com) with this line:
window.open('http://www.bugmenot.com/view.php?mo...=yes,menubars=no,toolbars=no,resizable=yes');
Then, in the new window I wish to automatically select the only text on
that page which changes (take a look at
http://www.bugmenot.com/view.php?mode=bookmarklet&url=http://www.nytimes.com

Wait, let me understand (I'm sorry, but my english is not very good)..

You have a page in a domain (http://www.nytimes.com); and you want open a
popup that points in other domain (http://www.bugmenot.com/), and after get
some information from this?

In according with SOP (Same Origin Policy), you cannot do it:

http://www.mozilla.org/projects/security/components/same-origin.html

--
ZER0

~ The Tangent Universe collapsed 5891 days, 9 hours, 26 minutes and 52 seconds ago.

on air ~ "donny darko - Mad World (full version)"
 
M

McKirahan

melvynadam said:
If I open a new page with very little text on it such as
a bugmenot
results page
is there a way to select some of the text there and
assign it to variables?
Selecting all and copying to the clipboard can be done with:
document.execCommand("SelectAll");
document.execCommand("Copy");
but I'm looking for something a little more refined.

The page contains seven lines of text and I'd like a simple way to say:
var username = [contents of line 4]
var password = [contents of line 5]
Can anyone point me in the right direction?

Will this help? Watch for word-wrap.

<html>
<head>
<title>bugmenot.htm</title>
<script type="text/javascript">
function bugmenot() {
var sWWW = http://www.bugmenot.com/view.php?url=;
var sURL = document.getElementById("URL").value;
var oXML = new ActiveXObject("Microsoft.XMLHTTP");
oXML.Open("GET",sWWW+sURL,false);
oXML.send();
try {
var sXML = oXML.ResponseText;
var iDD1 = sXML.indexOf("<dd>");
var iDD2 = sXML.indexOf("</dd>");
var sVAL = sXML.substr(iDD1+4,iDD2-iDD1-4);
var sUSR = sVAL.substr(0,sVAL.indexOf("<"));
var sPWD = sVAL.substr(sVAL.indexOf(">")+1);
document.getElementById("USR").value = sUSR;
document.getElementById("PWD").value = sPWD;
document.all.i.src = sWWW+sURL;
} catch(e) {
alert(sWWW + sURL + " Failed!");
}
}
</script>
</head>
<body>
<center>
<form>
<table border="0" width="600">
<td align="center">
<b>Enter a URL: </b>
<input type="text" name="URL" id="URL" size="30"
value="www.nytimes.com">
&nbsp;
<input type="button" value="Bug Me Not!" onclick="bugmenot()">
</td>
<tr>
</tr>
<td align="center">
<b>Username: </b>
<input type="text" name="USR" id="USR" readonly>
&nbsp;
<b>Password: </b>
<input type="text" name="PWD" id="PWD" readonly>
</td>
</tr>
</table>
</form>
<iframe name="i" align="center"
frameborder="1" scrolling="no"
marginwidth="0" marginheight="0"
style="width:600; height: 380"></iframe>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top