On Tue, Nov 24, 2009 at 5:39 AM, Jay <meat...@gmail.com> wrote:
> 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 received this message because you are subscribed to the Google Groups
> "Chromium-extensions" group.
> To post to this group, send email to chromium-extensions@googlegroups.com.
> To unsubscribe from this group, send email to
> chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/chromium-extensions?hl=en.
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);
> On Tue, Nov 24, 2009 at 5:39 AM, Jay <meat...@gmail.com> wrote:
> > 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 received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensions@googlegroups.com.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/chromium-extensions?hl=en.
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"});
On Tue, Nov 24, 2009 at 10:13 AM, Jay <meat...@gmail.com> wrote:
> 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);
> On Nov 24, 1:03 pm, Mohamed Mansour <m...@chromium.org> wrote:
>> In JS, you should use window.getSelection()
>> - Mohamed Mansour
>> On Tue, Nov 24, 2009 at 5:39 AM, Jay <meat...@gmail.com> wrote:
>> > 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 received this message because you are subscribed to the Google Groups
>> > "Chromium-extensions" group.
>> > To post to this group, send email to chromium-extensions@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com>
>> > .
>> > For more options, visit this group at
>> >http://groups.google.com/group/chromium-extensions?hl=en.
> --
> You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
> To post to this group, send email to chromium-extensions@googlegroups.com.
> To unsubscribe from this group, send email to chromium-extensions+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/chromium-extensions?hl=en.
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" });
};
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:
> 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"});
On Tue, Nov 24, 2009 at 1:04 PM, Erik Kay <erik...@google.com> wrote:
> Actually, Aaron's code will work fine as-is without a background page in
> the middle. Popups can talk to content scripts exactly the same way as the
> background page can.
On Tue, Nov 24, 2009 at 3:51 PM, Arne Roomann-Kurrik <kur...@google.com> wrote:
> Ah, figured there was probably a better way :)
> ~Arne
> On Tue, Nov 24, 2009 at 1:04 PM, Erik Kay <erik...@google.com> wrote:
>> Actually, Aaron's code will work fine as-is without a background page in
>> the middle. Popups can talk to content scripts exactly the same way as the
>> background page can.
>> Erik
> --
> You received this message because you are subscribed to the Google Groups
> "Chromium-extensions" group.
> To post to this group, send email to chromium-extensions@googlegroups.com.
> To unsubscribe from this group, send email to
> chromium-extensions+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/chromium-extensions?hl=en.
> 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:
> On Nov 24, 11:08 am, Aaron Boodman <a...@google.com> wrote:
> > 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"});
> 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:
> On Nov 24, 11:08 am, Aaron Boodman <a...@google.com> wrote:
> > 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"});
On Thu, Jan 21, 2010 at 4:07 AM, soupenvy <sweater...@gmail.com> wrote: > 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: > > 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:
> > On Nov 24, 11:08 am, Aaron Boodman <a...@google.com> wrote:
> > > 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"});
> > > I haven't tested any of this, but I think it should work :)
> > > - a
> -- > You received this message because you are subscribed to the Google Groups > "Chromium-extensions" group. > To post to this group, send email to chromium-extensions@googlegroups.com. > To unsubscribe from this group, send email to > chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/chromium-extensions?hl=en.
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:
> The sample still works for me in 4.0.295.0 (Official Build 35884) unstable
> Which version are you using?
> ~Arne
> On Thu, Jan 21, 2010 at 4:07 AM, soupenvy <sweater...@gmail.com> wrote: > > 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: > > > 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:
> > > > I haven't tested any of this, but I think it should work :)
> > > > - a
> > -- > > You received this message because you are subscribed to the Google Groups > > "Chromium-extensions" group. > > To post to this group, send email to chromium-extensions@googlegroups.com. > > To unsubscribe from this group, send email to > > chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/chromium-extensions?hl=en.
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.
On Thu, Jan 21, 2010 at 1:53 PM, Bill.Keller <sweater...@gmail.com> wrote: > 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: > > The sample still works for me in 4.0.295.0 (Official Build 35884) > unstable
> > Which version are you using?
> > ~Arne
> > On Thu, Jan 21, 2010 at 4:07 AM, soupenvy <sweater...@gmail.com> wrote: > > > 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: > > > > 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:
> > > > > I haven't tested any of this, but I think it should work :)
> > > > > - a
> > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Chromium-extensions" group. > > > To post to this group, send email to > chromium-extensions@googlegroups.com. > > > To unsubscribe from this group, send email to > > > chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com><chromium-extensions%2Bunsu > bscribe@googlegroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/chromium-extensions?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "Chromium-extensions" group. > To post to this group, send email to chromium-extensions@googlegroups.com. > To unsubscribe from this group, send email to > chromium-extensions+unsubscribe@googlegroups.com<chromium-extensions%2Bunsu bscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/chromium-extensions?hl=en.