Clipboard - Copy Text To Clipboard

M

Mahsha

<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
<!--

// Copyright (C) krikkit - (e-mail address removed)
// --> http://www.krikkit.net/
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

function copy_clip(txtObjId)
{
meintext = document.getElementById(txtObjId).value;
if (window.clipboardData)
{

// the IE-manier
window.clipboardData.setData("Text", meintext);

// waarschijnlijk niet de beste manier om Moz/NS te detecteren;
// het is mij echter onbekend vanaf welke versie dit precies werkt:
}
else if (window.netscape)
{

// dit is belangrijk maar staat nergens duidelijk vermeld:
// you have to sign the code to enable this, or see notes below
netscape.security.PrivilegeManager.enablePrivilege
('UniversalXPConnect');

// maak een interface naar het clipboard
var clip = Components.classes['@mozilla.org/widget/clipboard;1']
.createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;

// maak een transferable
var trans = Components.classes['@mozilla.org/widget/transferable;
1']
.createInstance
(Components.interfaces.nsITransferable);
if (!trans) return;

// specificeer wat voor soort data we op willen halen; text in dit
geval
trans.addDataFlavor('text/unicode');

// om de data uit de transferable te halen hebben we 2 nieuwe
objecten
// nodig om het in op te slaan
var str = new Object();
var len = new Object();

var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance
(Components.interfaces.nsISupportsString);

var copytext=meintext;

str.data=copytext;

trans.setTransferData("text/unicode",str,copytext.length*2);

var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false;

clip.setData(trans,null,clipid.kGlobalClipboard);

}
alert("Following info was copied to your clipboard:\n\n" +
meintext);
return false;
}
//-->
</script>
</head>

<body>
<textarea id="txtBox" style="width:300px; height:300px;"></textarea>
<a href="javascript:return copy_clip('txtBox');">Copy Text</a>
</body>
</html>
 
T

Thomas 'PointedEars' Lahn

Mahsha said:
<html>
<head>
<title></title>

<script language="javascript" type="text/javascript">

The `language' attribute is unnecessary, and deprecated since almost 10 years.

Unnecessary (HTML) to error-prone (XHTML).
// Copyright (C) krikkit - (e-mail address removed)
// --> http://www.krikkit.net/

If this was XHTML or content passed to a script engine implementing `<!--'
as comment, the comment would end here and the rest would be regarded a
syntax error by the script engine. So much for "krikkit".
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

Single-line comments should be used for disabling source code; multi-line
comments should be used for commenting on source code.
function copy_clip(txtObjId)
{
meintext = document.getElementById(txtObjId).value;

`meintext' is undeclared, so it becomes globally available (if that) with
known side-effects. Accessing the property of an object that is referred to
by the return value of a method directly is error-prone; always test your
references.
if (window.clipboardData)
{

// the IE-manier
window.clipboardData.setData("Text", meintext);

Missing feature test.
// waarschijnlijk niet de beste manier om Moz/NS te detecteren;
// het is mij echter onbekend vanaf welke versie dit precies werkt:
}
else if (window.netscape)

Yes, probably not the best method to determine Moz/NS nor is there a need
to. You are using netscape.security.PrivilegeManager below, so you should
feature-test, step by step, the references in this expression, such as

var sec, mgr;
if (typeof netscape != "undefined"
&& typeof netscape.security != "undefined"
&& typeof (sec = netscape.security).PrivilegeManager != "undefined"
&& typeof (mgr = sec.PrivilegeManager).enablePrivilege == "function")
{
// ...
}

Of course,

if (jsx.object.isMethod(_global.netscape, "security", "PrivilegeManager",
"enablePrivilege"))

would take care of all of that.
{

// dit is belangrijk maar staat nergens duidelijk vermeld:

It's explained on

// you have to sign the code to enable this, or see notes below

Yes, an alternative to signing the code is setting a user preference (ibid.)
[...]
// om de data uit de transferable te halen hebben we 2 nieuwe
objecten
// nodig om het in op te slaan
var str = new Object();
var len = new Object();

Nonsense, as `str' is cluelessly and unconditionally redeclared, and
overwritten below:
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance
(Components.interfaces.nsISupportsString);

This line suffices. The `len' variable declared and initialized above is
not even used, so it can and should be safely removed.
var copytext=meintext;
Unnecessary.

str.data=copytext;

str.data = meintext;
trans.setTransferData("text/unicode",str,copytext.length*2);

var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false;

clip.setData(trans,null,clipid.kGlobalClipboard);

}
alert("Following info was copied to your clipboard:\n\n" +
meintext);

Again the missing window.
return false;
}
//-->
Unnecessary.

</script>
</head>

<body>
<textarea id="txtBox" style="width:300px; height:300px;"></textarea>
<a href="javascript:return copy_clip('txtBox');">Copy Text</a>

See the FAQ about `javascript:' again. It's also nonsense to return
anything different from `undefined' there; the return value is supposed to
make up the temporary HTML document created by a `javascript:' URI, so that
would be a pointless HTML document containing only `false' here.

The following would have been better in HTML:

<script type="text/javascript">
document.write('<a href="javascript:void(copy_clip(\'txtBox\'))"'
+ ' onclick="return copy_clip(\'txtBox\')">Copy Text<\/a>');
</script>

If there was insufficient script support, the pseudo-link would not be
generated.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Mahsha wrote:

If this was XHTML or content passed to a script engine implementing `<!--'
as comment, the comment would end here and the rest would be regarded a
syntax error by the script engine. So much for "krikkit".

While I agree with the problem, by a funny coincidence, the above
won't necessarily give a syntax error. The string "http://..." is a
label followed by a single line comment. If there is a statement following
the label, the *syntax* is correct.

It's still very error prone. If what follows isn't a statement, but
could be seen as one, the label might change the semantics. E.g., a
strict implementation would fail on the following code:

http://www.example.com/...
function foo(){}
foo();

The http: label means that the following is a statement, not a
declaration, and as such, the function is not a function declaration
but a function expression as an expression-statement.
By correct ECMAScript semantics, "foo" would not be bound outside
of the function expression's body.

Most browsers don't follow the correct semantics here, though, and
declare foo anyway.


Just don't use <!-- in scripts.
/L
 
T

Thomas 'PointedEars' Lahn

Thomas said:
See the FAQ about `javascript:' again. It's also nonsense to return
anything different from `undefined' there; the return value is supposed to
make up the temporary HTML document created by a `javascript:' URI, so that
would be a pointless HTML document containing only `false' here.

It's even worse: `return' must not occur outside of a function, so it's not
even a valid/functional `javascript:' URI.
The following would have been better in HTML:
[...]


PointedEars
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top