A little issue while trying to develop a chrome extension. The target is simple and I know there are existing post on the matter but whatever I found did not work for me. It feels like what works in a normal script (html/js) does not work within the chrome extension.
I am trying to make a simple extension which on click on its icon will get the selection (at least text, if possible more info) but somehow it does not work. i get a selection Item but it is with an empty selection.
Here is the code. Manifest.json
{
"manifest_version": 2,
"name": "Paster",
"description": "Bla",
"version": "1.0",
"browser_action": {
"default_icon": "camera.png",
"default_title": "Bla"
},
"background": {
"page": "background.html"
},
"permissions": [
"tabs",
"activeTab",
"https://ajax.googleapis.com/",
"clipboardRead",
"clipboardWrite"
]
}
background.html
<textarea id="temp_textarea_draft_paster"></textarea>
<div>BLA BLA BLA</div>
<script src="jquery-3.1.1.min.js"></script>
<script src="background.js"></script>
background.js
chrome.browserAction.onClicked.addListener(function() {
getSelectionText()
});
getSelectionText = function (info, tab) {
selection = window.getSelection();
console.log(selection);
alert('yeah' + selection);
};
If I open the dev console from background.html I get the console.log but with an empty selection, whatever I have selected. The alert pops up too but just with 'yeah'.
if I go to the chrome-extensions://ext-id/background.html page and select a text there and click on the button, it works with a selection including its data... How come ? It feels like the extension does not get the selection from the tabs, just from the background.html page.