Why Chrome does not like this line of java script?

1,391 views
Skip to first unread message

jastrzebiec

unread,
Jul 7, 2010, 5:03:01 PM7/7/10
to Chromium-extensions
The line of js is:
Copied = document.forms("f1").holdtext.createTextRange

When I go to 'Inspect element', then click on Scripts there is Chrome
inserted orange bar: "Uncaught Type Error: Object #<an HTML
TextAreaElement> has no method 'create Text Range'

I do not have any such problems with IE.

Besides, I think there is Chrome bug.
When I highlight that orange error bar Chrome gives you 2 options (on
mouse right click):
- Copy
- Search Google for "'Uncaught Type Error...."

None of above menus works for me.

Any comments appreciated
jas

Arne Roomann-Kurrik

unread,
Jul 7, 2010, 5:27:21 PM7/7/10
to jastrzebiec, Chromium-extensions
What exactly are you trying to do?  The createTextRange function appears to be Internet Explorer-only.  Would using innerText or innerHTML instead let you do what you want?

~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Derek 囧

unread,
Jul 7, 2010, 5:30:58 PM7/7/10
to Chromium-extensions
I think you want to copy words that user selected.


On 7月7日, 下午4時27分, Arne Roomann-Kurrik <kur...@chromium.org> wrote:
> What exactly are you trying to do?  The createTextRange function appears to
> be Internet Explorer-only.  Would using innerText or innerHTML instead let
> you do what you want?
>
> ~Arne
>
> On Wed, Jul 7, 2010 at 2:03 PM, jastrzebiec <
>
>
>
> jackkozlow...@phonedialerpro.com> wrote:
> > The line of js is:
> > Copied = document.forms("f1").holdtext.createTextRange
>
> > When I go to 'Inspect element', then click on Scripts there is Chrome
> > inserted orange bar: "Uncaught Type Error: Object #<an HTML
> > TextAreaElement> has no method 'create Text Range'
>
> > I do not have any such problems with IE.
>
> > Besides, I think there is Chrome bug.
> > When I highlight that orange error bar Chrome gives you 2 options (on
> > mouse right click):
> > - Copy
> > - Search Google for "'Uncaught Type Error...."
>
> > None of above menus works for me.
>
> > Any comments appreciated
> > jas
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .

jastrzebiec

unread,
Jul 7, 2010, 11:39:42 PM7/7/10
to Chromium-extensions
Below is the full script:
It takes numbers inserted by the user and puts them on clipboard.
========================

<SCRIPT LANGUAGE="JavaScript">
function ClipBoard(){

flag = 1;
var acceptedChar = new String("0123456789") ;
var str1=document.forms("f1").T2.value;;
var digitsOnly=""
var i = 0;

for (i=0 ; i<str1.length; i++)
{
if ( acceptedChar.indexOf(str1.charAt(i))!= -1 )
{
digitsOnly=digitsOnly+str1.charAt(i);
}
}
document.forms("f1").holdtext.innerText = digitsOnly;
Copied = document.forms("f1").holdtext.createTextRange();
Copied.execCommand("RemoveFormat");
Copied.execCommand("Copy");
}
</SCRIPT>

Thanks,
jas

Adam

unread,
Jul 8, 2010, 10:47:24 AM7/8/10
to Chromium-extensions
execCommand("copy") doesn't work.

For right now, the only way to copy a string automatically from within
the extension is to give your extension experimental permissions in
the manifest.
You must also add the command --enable-experimental-extension-apis
flag to chrome.

I ran into this same problem.

Once you've done this, you can do a copy by calling
chrome.experimental.clipboard.executeCopy(int tabId, [function
callback]). That should be called by your background page, not a
content script.
Calling executeCopy will copy the selected text in the tab passed via
tabId. Its basically the same as hitting ctrl+C on that tab, except
this is done programatically.

The only way I know to copy a string to the clipboard is by creating
an input box, filling it with the string to be copied, then passing a
message to background.html, which then executes the copy. The input
box is then removed from the DOM when background.html send a message
back saying that the text was copied to the clipboard successfully.

I usually put the string to be copied in a input box inside an AP div
with dimensions 1px by 1px and opacity of .01. That prevents the user
from noticing the DOM 'jump' when the input box is inserted and
removed.

On Jul 7, 11:39 pm, jastrzebiec <jackkozlow...@phonedialerpro.com>
wrote:

Adam

unread,
Jul 8, 2010, 10:50:36 AM7/8/10
to Chromium-extensions
Also, you could simply the character removal via regex.

Something like str.replace(/[^0-9]/g, ""); That regex will remove all
characters from the string that are NOT a digit 0-9.

On Jul 7, 11:39 pm, jastrzebiec <jackkozlow...@phonedialerpro.com>
wrote:
Reply all
Reply to author
Forward
0 new messages