Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with Chrome Smiley App

    Hi, Im trying to build a Chrome application which builds a button in the toolbar. When this button is clicked, I want to get a list of smileys as buttons. That part is ready. However, I have a problem with the Javascript needed to add to every button. I want the button to write the smiley's text-form in any input or textarea element which was focused before I clicked on the button. Can anyone help me with the JS code I need to do this?

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Code:
    function injectText(ta, strText) {  
    
    	
    	var s=ta.selectionStart, 
    	e=ta.selectionEnd, 
    	v=ta.value, 
    	x=v.slice(s,e),
    	t=ta.scrollTop;
    	
    	ta.value=v.slice(0,s) +
    	         x +
    	         strText +
    	         v.slice(e);
    	         
    	ta.selectionStart = s+x.length;
    	ta.selectionEnd = s+x.length+strText.length;
    	setTimeout(function(){ta.focus();}, 100);
    	ta.scrollTop = t;
    }
    
    //example:
    injectText( myTA, ":)" );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried this code:
    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    			function injectText(ta, strText) {  
    
    	
    	var s=ta.selectionStart, 
    	e=ta.selectionEnd, 
    	v=ta.value, 
    	x=v.slice(s,e),
    	t=ta.scrollTop;
    	
    	ta.value=v.slice(0,s) +
    	         x +
    	         strText +
    	         v.slice(e);
    	         
    	ta.selectionStart = s+x.length;
    	ta.selectionEnd = s+x.length+strText.length;
    	setTimeout(function(){ta.focus();}, 100);
    	ta.scrollTop = t;
    }
    		</script>
    	</head>
    	<body>
    <a href="#" onClick="injectText(myTA, ':)');">:)</a>
    	</body>
    </html>
    However, this didn't work. I am trying to write the smiley in a text field which does not have to do with the app. It can be any page. What's wrong with this?

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    i dont see a textarea in that example. if you had one with an ID of myTA, ti would work.
    You need to pass a ref to the textarea as arg0, and the text as arg1.

    for example, from firebug, we can use the script on this very page, injecting a frowny into the reply box i'm using right now:

    Code:
    injectText(document.getElementById('vB_Editor_QR_textarea'), ' :( ' );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    i dont see a textarea in that example. if you had one with an ID of myTA, ti would work.
    You need to pass a ref to the textarea as arg0, and the text as arg1.
    That is the problem. I want it to write the smiley on a textarea which is not in the app itself. Can this be done?

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Quote Originally Posted by Liongold View Post
    That is the problem. I want it to write the smiley on a textarea which is not in the app itself. Can this be done?
    i don't know what that means, but you have working code so try it.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I know it's a little confusing but let me try by example. Imagine I have Chrome and installed the app. This app gives me a button next to the "Options" one and when I press it, it opens app.html which contains a set of buttons containing smileys. Now, imagine I open a tab and go to www.example.com and there is a textarea in this page. My aim is to use the app to put a smiley in the www.example.com textarea. Can this be done, please?

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    ahh, that makes sense, but sadly, i don't know much about extensions. I would think they could interact with the page, and if so, there's got to be an example or two floating around. I would imagine that you have to do something like contentWindow.getElementsByTagName("textarea"), but i don't know exactly how to reach web-page content from the extension... Still, i am 90% sure it's possible.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •