Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
passing user highlighted webpage text to a browser_action
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jay  
View profile  
 More options Nov 24 2009, 5:39 am
From: Jay <meat...@gmail.com>
Date: Tue, 24 Nov 2009 02:39:28 -0800 (PST)
Local: Tues, Nov 24 2009 5:39 am
Subject: passing user highlighted webpage text to a browser_action
Hey guys,

Any ideas on the best way to do this?  In regular JS i'd use
content.getSelection().  A little lost on how to do it here.

Thanks in advance.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mohamed Mansour  
View profile  
 More options Nov 24 2009, 1:03 pm
From: Mohamed Mansour <m...@chromium.org>
Date: Tue, 24 Nov 2009 13:03:39 -0500
Local: Tues, Nov 24 2009 1:03 pm
Subject: Re: [chromium-extensions] passing user highlighted webpage text to a browser_action

In JS, you should use window.getSelection()

 - Mohamed Mansour


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jay  
View profile  
 More options Nov 24 2009, 1:13 pm
From: Jay <meat...@gmail.com>
Date: Tue, 24 Nov 2009 10:13:37 -0800 (PST)
Local: Tues, Nov 24 2009 1:13 pm
Subject: Re: passing user highlighted webpage text to a browser_action
I don't think that quite works in this context.  My function
(browser_action: popup.html):

<script>
        var selected_txt = window.getSelection();  <--- does not work

        function shareaholic(){
                chrome.tabs.getSelected( null , function(tab) {
                        var shareaholicUrl = "http://www.shareaholic.com/share/";
                        var url = encodeURIComponent(tab.url);
                        var title = encodeURIComponent(tab.title);

                        var shareaholicUrl_tab = shareaholicUrl + '?url=' + url + '&title='
+ title +'&note=' + selected_txt;
                        var shr_iframe = '<iframe id="shrFrame" name="shrFrame" src="' +
shareaholicUrl_tab + '" noresize="noresize" frameborder="0"></
iframe>';
                        document.getElementById('shr_iframe').innerHTML = shr_iframe;
        });

}

</script>

On Nov 24, 1:03 pm, Mohamed Mansour <m...@chromium.org> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Boodman  
View profile  
 More options Nov 24 2009, 2:08 pm
From: Aaron Boodman <a...@google.com>
Date: Tue, 24 Nov 2009 11:08:51 -0800
Local: Tues, Nov 24 2009 2:08 pm
Subject: Re: [chromium-extensions] Re: passing user highlighted webpage text to a browser_action
You can setup a content script that does it for you that your popup
communicates with. I do it with three files:

background.html (register this in your manifest with the background_page key):
=============
function getSelection() {
  chrome.tabs.executeScript(null,  // by default, executes in current tab
    {  file: "content_script.js"});

}

chrome.extension.onRequest.addListener(function(request) {
  alert("got selection: " + request);

});

content_script.js
============
chrome.extension.sendRequest(window.getSelection());

popup.html
========
chrome.extension.getBackgroundPage().getSelection();

I haven't tested any of this, but I think it should work :)

- a


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arne Roomann-Kurrik  
View profile  
 More options Nov 24 2009, 2:52 pm
From: Arne Roomann-Kurrik <kur...@google.com>
Date: Tue, 24 Nov 2009 11:52:29 -0800 (PST)
Local: Tues, Nov 24 2009 2:52 pm
Subject: Re: passing user highlighted webpage text to a browser_action
As a note, you'll need to convert the result of getSelection() to a
string or else it won't serialize correctly for the sendRequest call.
window.getSelection().toString() is probably easiest.

If you want to get access to the selected text in a popup, you'll need
to pass it onward from the background page.  There's probably a better
way to do this, but here's a background page which forwards the
message:

<html>
  <head>
    <script type="text/javascript">
      var selection_callbacks = [];

      function getSelection(callback) {
        selection_callbacks.push(callback);
        chrome.tabs.executeScript(null, { file:
"contentscript.js" });
      };

      chrome.extension.onRequest.addListener(function (request) {
        var callback = selection_callbacks.shift();
        callback(request);
      });
    </script>
  </head>
  <body>
  </body>
</html>

and here's a popup which displays it (it's probably not a great idea
to just innerHTML the text, but this is just an example):

<html>
  <head>
    <script type="text/javascript">
      function onSelection(text) {
        document.getElementById("output").innerHTML = text;
      }
      chrome.extension.getBackgroundPage().getSelection(onSelection);
    </script>
  </head>
  <body>
    <div id="output">
      This should be replaced with the selected text
    </div>
  </body>
</html>

The content script is almost identical to Aaron's:

chrome.extension.sendRequest(window.getSelection().toString());

~Arne

On Nov 24, 11:08 am, Aaron Boodman <a...@google.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arne Roomann-Kurrik  
View profile  
 More options Nov 24 2009, 6:51 pm
From: Arne Roomann-Kurrik <kur...@google.com>
Date: Tue, 24 Nov 2009 15:51:49 -0800
Local: Tues, Nov 24 2009 6:51 pm
Subject: Re: [chromium-extensions] Re: passing user highlighted webpage text to a browser_action

Ah, figured there was probably a better way :)

~Arne


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Boodman  
View profile  
 More options Nov 24 2009, 6:53 pm
From: Aaron Boodman <a...@google.com>
Date: Tue, 24 Nov 2009 15:53:28 -0800
Local: Tues, Nov 24 2009 6:53 pm
Subject: Re: [chromium-extensions] Re: passing user highlighted webpage text to a browser_action
I was worried about the popup going away while the IPC is outstanding.

- a


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jay  
View profile  
 More options Nov 29 2009, 6:08 pm
From: Jay <meat...@gmail.com>
Date: Sun, 29 Nov 2009 15:08:55 -0800 (PST)
Local: Sun, Nov 29 2009 6:08 pm
Subject: Re: passing user highlighted webpage text to a browser_action
Arne, Aaron,

Thanks so much -- this works great!

Jay

On Nov 24, 2:52 pm, Arne Roomann-Kurrik <kur...@google.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
soupenvy  
View profile  
 More options Jan 21 2010, 7:07 am
From: soupenvy <sweater...@gmail.com>
Date: Thu, 21 Jan 2010 04:07:35 -0800 (PST)
Local: Thurs, Jan 21 2010 7:07 am
Subject: Re: passing user highlighted webpage text to a browser_action
Is there any particular change in Chrome that prevents this from
working in the latest builds?

I can't seem to get this sample working.

On Nov 24 2009, 2:52 pm, Arne Roomann-Kurrik <kur...@google.com>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arne Roomann-Kurrik  
View profile  
 More options Jan 21 2010, 1:45 pm
From: Arne Roomann-Kurrik <kur...@google.com>
Date: Thu, 21 Jan 2010 10:45:38 -0800
Local: Thurs, Jan 21 2010 1:45 pm
Subject: Re: [crx] Re: passing user highlighted webpage text to a browser_action

The sample still works for me in 4.0.295.0 (Official Build 35884) unstable

Which version are you using?

~Arne


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill.Keller  
View profile  
 More options Jan 21 2010, 4:53 pm
From: "Bill.Keller" <sweater...@gmail.com>
Date: Thu, 21 Jan 2010 13:53:20 -0800 (PST)
Local: Thurs, Jan 21 2010 4:53 pm
Subject: Re: passing user highlighted webpage text to a browser_action
Chromium 4.0.303.0 (36626) Mac
Chrome 4.0.295.0 dev Mac
Chrome 4.0.295.0 dev PC

It seems as though the content_script only runs once on each page
load. It's not continuously sending the selected txt to the background
page.

For example, the below content_script reveals that the only way to
actually capture the selected text is to quickly select some text
before a page finishes loading.
chrome.extension.sendRequest(window.getSelection().toString());
console.log(window.getSelection().toString());

On Jan 21, 1:45 pm, Arne Roomann-Kurrik <kur...@google.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arne Roomann-Kurrik  
View profile  
 More options Jan 21 2010, 4:59 pm
From: Arne Roomann-Kurrik <kur...@google.com>
Date: Thu, 21 Jan 2010 13:59:14 -0800
Local: Thurs, Jan 21 2010 4:59 pm
Subject: Re: [crx] Re: passing user highlighted webpage text to a browser_action

Aaron's code uses chrome.tabs.executeScript to dynamically inject the
content script when the popup is opened - it's not included by default
through the manifest.

~Arne


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »