basic message passing

This commit is contained in:
Sriram Hariharan
2018-07-09 13:22:33 -05:00
parent 78df19b304
commit 659848d65f
3 changed files with 29 additions and 28 deletions

View File

@@ -1,19 +1,25 @@
chrome.runtime.onMessage.addListener(function(request, sender, response) {
const xhr = new XMLHttpRequest();
const method = request.method ? request.method.toUpperCase() : "GET";
xhr.open(method, request.url, true);
xhr.onload = () => response(xhr.responseText);
xhr.onerror = () => response(xhr.statusText);
if (method == "POST") {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if(request.greeting == "hello") {
getSaved(request,sender,response);
}
else{
const xhr = new XMLHttpRequest();
const method = request.method ? request.method.toUpperCase() : "GET";
xhr.open(method, request.url, true);
xhr.onload = () => response(xhr.responseText);
xhr.onerror = () => response(xhr.statusText);
if (method == "POST") {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
xhr.send(request.data);
}
xhr.send(request.data);
return true;
});
chrome.runtime.onInstalled.addListener(function() {
chrome.storage.sync.set({color: '#3aa757'}, function() {
console.log("The color is green.");
});
});
function getSaved(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
sendResponse({farewell: "goodbye"});
}