Skip to content

Instantly share code, notes, and snippets.

@sax1johno
Forked from anonymous/index.html
Last active December 30, 2015 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sax1johno/a8686342b27bb0f115c9 to your computer and use it in GitHub Desktop.
Save sax1johno/a8686342b27bb0f115c9 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/xemicefoxi
// Chrome extension button / popup / requester
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(
tab.id, // id of the tab to send a request to.
{method: "getHighlighted", name: "John"}, // data to send to the tab
function(response) { // function to execute when the tab responds.
if (response.method == 'getHighlighted') {
chrome.setClipboard(response.data); // Something like this.
// response.data contains our highlighted text.
}
else if (response.method == 'someOtherMethod') {
// do something else if this is a response to another method.
}
});
});
// Content Script / Responder
// Request listener goes here.
// Gets installed as part of the extension and runs in the background
chrome.extension.onRequest.addListener(function(request, sender, response) {
// This is a request listener.
// Only act if the request method is actually what we want to respond to.
if (request.method == "getHighlighted") {
// Send a response to the request
sendResponse({method: request.method, data: document.all[0].getHighlightedText()});
}
});
chrome.extension.onRequest.addListener(function(request, sender, response) {
// This is a totally different request listener.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment