Created
February 12, 2012 00:13
-
-
Save mohamedmansour/1805261 to your computer and use it in GitHub Desktop.
Chrome Extension to transfer the current selected webpage text to the popup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { | |
if (request.method == 'getSelection') { | |
sendResponse({data: window.getSelection().toString()}); | |
} | |
else { | |
sendResponse({}); // snub them. | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the future viewers. Don't use onRequest and tabs.getSelected. They are deprecated by Google.