From 659848d65fabf82144d39ad8e57e5f1acc46bc13 Mon Sep 17 00:00:00 2001 From: Sriram Hariharan Date: Mon, 9 Jul 2018 13:22:33 -0500 Subject: [PATCH] basic message passing --- background.js | 32 +++++++++++++++++++------------- content.js | 23 +++++++++-------------- styles.css | 2 +- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/background.js b/background.js index 015b8d35..872a4f5b 100644 --- a/background.js +++ b/background.js @@ -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"}); +} \ No newline at end of file diff --git a/content.js b/content.js index e791c2eb..11248dc5 100644 --- a/content.js +++ b/content.js @@ -46,6 +46,9 @@ $(document).ready( function() { getDistribution(); }); $("#saveCourse").click(function(){ +chrome.runtime.sendMessage({greeting: "hello"}, function(response) { + console.log(response.farewell); +}); }); $("#Syllabi").click(function(){ @@ -151,10 +154,6 @@ else{ } var building = place.substring(0,place.search(/\d/)-1); return output + " at "+time.replace(/\./g,'').replace(/\-/g,' to ')+" in "+""+place.substring(0,place.search(/\d/)-1)+""; -} - -function order(){ - } function getDistribution(){ var query = "select * from agg"; @@ -192,7 +191,7 @@ var color = "black"; else if(status.includes("closed") || status.includes("cancelled")){ color = "#F44336"; } -$(".title").append(""+" #"+uniquenum+""); +$(".title").append(""+" #"+uniquenum+""); var name; if(profname == ""){ name = "Undecided Professor "; @@ -201,10 +200,10 @@ if(profname == ""){ } } else{ - name = profname.substring(0,1)+profname.substring(1).toLowerCase(); + name = profinit+". "+profname.substring(0,1)+profname.substring(1).toLowerCase(); } -$(".profname").html("

with "+""+name+"