window.getSelection() returns an empty string on Chrome when a word is selected Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to access the webpage DOM rather than the extension page DOM?How to check empty/undefined/null string in JavaScript?How to execute a JavaScript function when I have its name as a stringselection and site search in chrome extensionWhy does ++[[]][+[]]+[+[]] return the string “10”?Chrome Extension: create new tab(chrome.tabs.create) and executeScript in new tab…not working :(How to get current tab url while opening the popup [without tabs permission]Chrome extension: ContextMenu Right click link instead of selectiontext?I can not get in the h1 headingCapture text selection in Google DocsHow to get string from 'window.getSelection' and how to fix opening new tab by using 'window.open'?
How do I check if a string is entirely made of the same substring?
Map material from china not allowed to leave the country
How long after the last departure shall the airport stay open for an emergency return?
Mistake in years of experience in resume?
Error: Syntax error. Missing ')' for CASE Statement
Can I criticise the more senior developers around me for not writing clean code?
Why did Israel vote against lifting the American embargo on Cuba?
What is the best way to deal with NPC-NPC combat?
Is it acceptable to use working hours to read general interest books?
Is accepting an invalid credit card number a security issue?
"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"
Did the Roman Empire have penal colonies?
What is this word supposed to be?
What is the ongoing value of the Kanban board to the developers as opposed to management
Where did Arya get these scars?
Can you stand up from being prone using Skirmisher outside of your turn?
Multiple fireplaces in an apartment building?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
Retract an already submitted recommendation letter (written for an undergrad student)
All ASCII characters with a given bit count
c++ diamond problem - How to call base method only once
Is it OK if I do not take the receipt in Germany?
Will I lose my paid in full property
window.getSelection() returns an empty string on Chrome when a word is selected
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to access the webpage DOM rather than the extension page DOM?How to check empty/undefined/null string in JavaScript?How to execute a JavaScript function when I have its name as a stringselection and site search in chrome extensionWhy does ++[[]][+[]]+[+[]] return the string “10”?Chrome Extension: create new tab(chrome.tabs.create) and executeScript in new tab…not working :(How to get current tab url while opening the popup [without tabs permission]Chrome extension: ContextMenu Right click link instead of selectiontext?I can not get in the h1 headingCapture text selection in Google DocsHow to get string from 'window.getSelection' and how to fix opening new tab by using 'window.open'?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to make a simple extension, where the user can do a dictionary search by hitting a shortcut. It will open a new tab with the dictionary search of the word selected by the user. However, when I try to use "window.getSelection()", it returns an empty string. I am using the Chrome browser, latest version.
This is the background2.js file.
chrome.commands.onCommand.addListener(function(command)
console.log('onCommand event received for message: ', command);
chrome.tabs.create(
url: "https://jisho.org/search/" + encodeURIComponent(window.getSelection().toString()),
active: true
);
);
This is the manifest.json file.
"manifest_version": 2,
"name": "Jisho Dictionary Search",
"permissions": [ "contextMenus" , "tabs" ],
"version": "1.2",
"background":
"scripts": [ "background.js", "background2.js" ]
,
"description": "Adds a shortcut to do a Jisho search automatically",
"browser_action":
"default_popup" : "browser_action.html"
,
"commands":
"toggle_feature":
"suggested_key": "default": "Ctrl+X" ,
"description" : "Opens new tab with word search on Jisho"
,
"_execute_browser_action":
"suggested_key":
"default": "Ctrl+Shift+X"
Can someone help me understand why it is returning an empty string and how could I make it return the word the user selected?
javascript
![](http://i.stack.imgur.com/EdUwb.png)
add a comment |
I am trying to make a simple extension, where the user can do a dictionary search by hitting a shortcut. It will open a new tab with the dictionary search of the word selected by the user. However, when I try to use "window.getSelection()", it returns an empty string. I am using the Chrome browser, latest version.
This is the background2.js file.
chrome.commands.onCommand.addListener(function(command)
console.log('onCommand event received for message: ', command);
chrome.tabs.create(
url: "https://jisho.org/search/" + encodeURIComponent(window.getSelection().toString()),
active: true
);
);
This is the manifest.json file.
"manifest_version": 2,
"name": "Jisho Dictionary Search",
"permissions": [ "contextMenus" , "tabs" ],
"version": "1.2",
"background":
"scripts": [ "background.js", "background2.js" ]
,
"description": "Adds a shortcut to do a Jisho search automatically",
"browser_action":
"default_popup" : "browser_action.html"
,
"commands":
"toggle_feature":
"suggested_key": "default": "Ctrl+X" ,
"description" : "Opens new tab with word search on Jisho"
,
"_execute_browser_action":
"suggested_key":
"default": "Ctrl+Shift+X"
Can someone help me understand why it is returning an empty string and how could I make it return the word the user selected?
javascript
![](http://i.stack.imgur.com/EdUwb.png)
The background script runs in a separate hidden background page. You need to run a content script to access the web page's selection and pass it to the background script via messaging or executeScript's callback as shown in other answers and examples. See also Where to read console messages from background.js in a Chrome extension?
– wOxxOm
Mar 22 at 16:27
@wOxxOm Thank you, it works now with messaging.
– Pedro Lopes
Mar 22 at 18:06
add a comment |
I am trying to make a simple extension, where the user can do a dictionary search by hitting a shortcut. It will open a new tab with the dictionary search of the word selected by the user. However, when I try to use "window.getSelection()", it returns an empty string. I am using the Chrome browser, latest version.
This is the background2.js file.
chrome.commands.onCommand.addListener(function(command)
console.log('onCommand event received for message: ', command);
chrome.tabs.create(
url: "https://jisho.org/search/" + encodeURIComponent(window.getSelection().toString()),
active: true
);
);
This is the manifest.json file.
"manifest_version": 2,
"name": "Jisho Dictionary Search",
"permissions": [ "contextMenus" , "tabs" ],
"version": "1.2",
"background":
"scripts": [ "background.js", "background2.js" ]
,
"description": "Adds a shortcut to do a Jisho search automatically",
"browser_action":
"default_popup" : "browser_action.html"
,
"commands":
"toggle_feature":
"suggested_key": "default": "Ctrl+X" ,
"description" : "Opens new tab with word search on Jisho"
,
"_execute_browser_action":
"suggested_key":
"default": "Ctrl+Shift+X"
Can someone help me understand why it is returning an empty string and how could I make it return the word the user selected?
javascript
![](http://i.stack.imgur.com/EdUwb.png)
I am trying to make a simple extension, where the user can do a dictionary search by hitting a shortcut. It will open a new tab with the dictionary search of the word selected by the user. However, when I try to use "window.getSelection()", it returns an empty string. I am using the Chrome browser, latest version.
This is the background2.js file.
chrome.commands.onCommand.addListener(function(command)
console.log('onCommand event received for message: ', command);
chrome.tabs.create(
url: "https://jisho.org/search/" + encodeURIComponent(window.getSelection().toString()),
active: true
);
);
This is the manifest.json file.
"manifest_version": 2,
"name": "Jisho Dictionary Search",
"permissions": [ "contextMenus" , "tabs" ],
"version": "1.2",
"background":
"scripts": [ "background.js", "background2.js" ]
,
"description": "Adds a shortcut to do a Jisho search automatically",
"browser_action":
"default_popup" : "browser_action.html"
,
"commands":
"toggle_feature":
"suggested_key": "default": "Ctrl+X" ,
"description" : "Opens new tab with word search on Jisho"
,
"_execute_browser_action":
"suggested_key":
"default": "Ctrl+Shift+X"
Can someone help me understand why it is returning an empty string and how could I make it return the word the user selected?
javascript
![](http://i.stack.imgur.com/EdUwb.png)
javascript
![](http://i.stack.imgur.com/EdUwb.png)
asked Mar 22 at 15:48
![](https://lh4.googleusercontent.com/-sn9mBsI_0qE/AAAAAAAAAAI/AAAAAAAAA7I/ol6q86xyO4s/photo.jpg?sz=32)
![](https://lh4.googleusercontent.com/-sn9mBsI_0qE/AAAAAAAAAAI/AAAAAAAAA7I/ol6q86xyO4s/photo.jpg?sz=32)
Pedro LopesPedro Lopes
61
61
The background script runs in a separate hidden background page. You need to run a content script to access the web page's selection and pass it to the background script via messaging or executeScript's callback as shown in other answers and examples. See also Where to read console messages from background.js in a Chrome extension?
– wOxxOm
Mar 22 at 16:27
@wOxxOm Thank you, it works now with messaging.
– Pedro Lopes
Mar 22 at 18:06
add a comment |
The background script runs in a separate hidden background page. You need to run a content script to access the web page's selection and pass it to the background script via messaging or executeScript's callback as shown in other answers and examples. See also Where to read console messages from background.js in a Chrome extension?
– wOxxOm
Mar 22 at 16:27
@wOxxOm Thank you, it works now with messaging.
– Pedro Lopes
Mar 22 at 18:06
The background script runs in a separate hidden background page. You need to run a content script to access the web page's selection and pass it to the background script via messaging or executeScript's callback as shown in other answers and examples. See also Where to read console messages from background.js in a Chrome extension?
– wOxxOm
Mar 22 at 16:27
The background script runs in a separate hidden background page. You need to run a content script to access the web page's selection and pass it to the background script via messaging or executeScript's callback as shown in other answers and examples. See also Where to read console messages from background.js in a Chrome extension?
– wOxxOm
Mar 22 at 16:27
@wOxxOm Thank you, it works now with messaging.
– Pedro Lopes
Mar 22 at 18:06
@wOxxOm Thank you, it works now with messaging.
– Pedro Lopes
Mar 22 at 18:06
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303332%2fwindow-getselection-returns-an-empty-string-on-chrome-when-a-word-is-selected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303332%2fwindow-getselection-returns-an-empty-string-on-chrome-when-a-word-is-selected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The background script runs in a separate hidden background page. You need to run a content script to access the web page's selection and pass it to the background script via messaging or executeScript's callback as shown in other answers and examples. See also Where to read console messages from background.js in a Chrome extension?
– wOxxOm
Mar 22 at 16:27
@wOxxOm Thank you, it works now with messaging.
– Pedro Lopes
Mar 22 at 18:06