Skip to content

Commit

Permalink
query the background to see if logged in from content script and noti…
Browse files Browse the repository at this point in the history
…fy interested tabs when auth state changes
  • Loading branch information
typotter committed Sep 1, 2017
1 parent 824f22b commit ca7676c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 8 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ window.onload = function() {
initApp();
};

function notifyOnAuth(tabId) {
firebase.auth().onAuthStateChanged(function(user) {
console.log("auth state changed; now notify!");
chrome.tabs.sendMessage(tabId, {action: "AUTH_STATUS", state: !!user});
});
}

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
console.log("Received %o from %o, frame", msg, sender.tab, sender.frameId);
switch (msg.action) {
case "AUTH_STATUS":
notifyOnAuth(sender.tab.id);
sendResponse(fbAuthenticated);
break;
case "GET":
Expand Down
29 changes: 26 additions & 3 deletions content_script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
var isAuthed = false;


chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("CS Received %o from %o, frame", request, sender.tab, sender.frameId);
if (request.action == "AUTH_CHANGE") {
console.log("Auth state changed", request.state);
isAuthed = request.state;
}
});


chrome.runtime.sendMessage({action: "AUTH_STATUS"}, function(response) {
console.log("Auth Status response", response);
isAuthed = response;
});


var injectScript = function(func) {
var actualCode = '(' + func + ')();';
var script = document.createElement('script');
Expand Down Expand Up @@ -77,10 +96,14 @@ var loadBatchPageOverlay = function(domRoot) {
}


chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {




console.log("CS Received %o from %o, frame", request, sender.tab, sender.frameId);
if (request.action == "AUTH_CHANGE") {
console.log("Auth state changed", request.state);
}
});



Expand Down

0 comments on commit ca7676c

Please sign in to comment.