Skip to content

Instantly share code, notes, and snippets.

Created December 30, 2015 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/355a831f0023d1a662fc to your computer and use it in GitHub Desktop.
Save anonymous/355a831f0023d1a662fc to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xemicefoxi
<!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>
// 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