Created
December 30, 2015 00:58
-
-
Save anonymous/355a831f0023d1a662fc to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xemicefoxi
This file contains 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> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// 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. | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// 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. | |
});</script></body> | |
</html> |
This file contains 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 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