Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created February 12, 2012 00:13
Show Gist options
  • Save mohamedmansour/1805261 to your computer and use it in GitHub Desktop.
Save mohamedmansour/1805261 to your computer and use it in GitHub Desktop.
Chrome Extension to transfer the current selected webpage text to the popup
{
"name": "Selected Text",
"version": "0.1",
"description": "Selected Text",
"browser_action": {
"default_title": "Selected Text",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["selection.js"],
"run_at": "document_start",
"all_frames": true
}
]
}
<!DOCTYPE html>
<html>
<style>
body { width: 300px; height: 100px; margin: 0; padding: 0;}
textarea { width: 295px; margin: 0; padding: 0; height: 100px; }
</style>
<textarea id="text"> </textarea>
<script>
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {method: 'getSelection'}, function (response) {
var text = document.getElementById('text');
text.value = response.data;
});
});
</script>
</html>
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == 'getSelection') {
sendResponse({data: window.getSelection().toString()});
}
else {
sendResponse({}); // snub them.
}
});
@kutsan
Copy link

kutsan commented Sep 11, 2016

For the future viewers. Don't use onRequest and tabs.getSelected. They are deprecated by Google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment