Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "Use My Current Account",
"version": "1.3",
"version": "1.4",
"description": "Use the browser's profile when signing into Microsoft sites.",
"homepage_url": "https://github.com/novotnyllc/UseMyCurrentAccount",
"permissions": [
"identity",
"identity.email",
"storage",
"webRequest",
"webRequestBlocking",
"declarativeNetRequestWithHostAccess"
],
"host_permissions": [
"*://login.microsoftonline.com/*"
],
"author": "Claire Novotny",
Expand All @@ -19,16 +20,15 @@
"128": "icons/icon_128.png"
},
"background": {
"persistent": true,
"scripts": [ "src/background.js" ]
"service_worker": "src/background.js"
},
"browser_action": {
"action": {
"default_icon": {
"16": "icons/icon_16.png",
"20": "icons/icon_20.png",
"48": "icons/icon_48.png",
"128": "icons/icon_128.png"
}
},
"manifest_version": 2
"manifest_version": 3
}
167 changes: 123 additions & 44 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,130 @@
let email = '';
let useCurrentAccount = true;

chrome.identity.getProfileUserInfo(function(userInfo) { email = userInfo.email; });
// Debug flag - set to false to disable logging in production
const DEBUG = false;

useCurrentAccount = true;
// Debug logging helper
function debugLog(message, ...args) {
if (DEBUG) {
console.log(`[UseMyCurrentAccount] ${message}`, ...args);
}
}

function debugWarn(message, ...args) {
if (DEBUG) {
console.warn(`[UseMyCurrentAccount] ${message}`, ...args);
}
}

function debugError(message, ...args) {
// Always log errors, even in production
console.error(`[UseMyCurrentAccount] ${message}`, ...args);
}

// Function to get email with promise support
async function getEmail() {
if (email) return email;

try {
const userInfo = await chrome.identity.getProfileUserInfo();
if (userInfo && userInfo.email) {
email = userInfo.email;
debugLog('Email loaded:', email);
return email;
} else {
debugWarn('No email found in userInfo');
return '';
}
} catch (error) {
debugError('Error getting profile:', error);
return '';
}
}

getState(function(state) {
updateIcon(state);
// Initialize on service worker startup
getEmail().then(() => {
getState(function(state) {
updateIcon(state);
});
});

chrome.webRequest.onBeforeRequest.addListener(function(details){

if(useCurrentAccount === true ){
var url = new URL(details.url);
// Use declarativeNetRequest to redirect login URLs
async function updateRedirectRules() {
const userEmail = await getEmail();

if (!userEmail || !useCurrentAccount) {
// Remove all rules when disabled or no email
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, 2]
});
return;
}

var search = url.searchParams;
if(!search.has("login_hint") && !search.has("sid")){
search.append("login_hint", email);

return { redirectUrl: url.toString()};
}
}
},
{urls:["*://login.microsoftonline.com/*/authorize*"]},
["blocking"]
);

chrome.webRequest.onBeforeRequest.addListener(function(details){

if(useCurrentAccount === true ){
var url = new URL(details.url);

var search = url.searchParams;
if(!search.has("whr")) {

var domain = email.split('@').pop()
search.append("whr", domain);

return { redirectUrl: url.toString()};
}
}
},
{urls:["*://login.microsoftonline.com/*/saml2*",
"*://login.microsoftonline.com/*/wsfed*"]},
["blocking"]
);
const domain = userEmail.split('@').pop();

// Rule 1: Add login_hint parameter to /authorize URLs
const rule1 = {
id: 1,
priority: 1,
action: {
type: 'redirect',
redirect: {
transform: {
queryTransform: {
addOrReplaceParams: [
{ key: 'login_hint', value: userEmail }
]
}
}
}
},
condition: {
urlFilter: '||login.microsoftonline.com/*/authorize',
resourceTypes: ['main_frame', 'sub_frame']
}
};

// Rule 2: Add whr parameter to /saml2 and /wsfed URLs
const rule2 = {
id: 2,
priority: 1,
action: {
type: 'redirect',
redirect: {
transform: {
queryTransform: {
addOrReplaceParams: [
{ key: 'whr', value: domain }
]
}
}
}
},
condition: {
regexFilter: '^https://login\\.microsoftonline\\.com/.*/(?:saml2|wsfed)',
resourceTypes: ['main_frame', 'sub_frame']
}
};

try {
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1, 2],
addRules: [rule1, rule2]
});
debugLog('Redirect rules updated successfully');
} catch (error) {
debugError('Error updating redirect rules:', error);
}
}

function setState(state){
useCurrentAccount = state;
chrome.storage.local.set({
state: state
});
// Update rules when state changes
updateRedirectRules();
}

function getState(callback) {
Expand All @@ -64,11 +140,14 @@ function getState(callback) {
});
}

getState(function(state) {
updateIcon(state);
// Update rules when extension loads
getEmail().then(() => {
getState(function(state) {
updateRedirectRules();
});
});

chrome.browserAction.onClicked.addListener(function() {
chrome.action.onClicked.addListener(function() {
getState(function(state) {
var newState = !state;
updateIcon(newState);
Expand All @@ -79,11 +158,11 @@ chrome.browserAction.onClicked.addListener(function() {
function updateIcon(state) {
var color = [255, 0, 0, 255];
var text = state ? '' : 'Off';
chrome.browserAction.setBadgeBackgroundColor({
chrome.action.setBadgeBackgroundColor({
color: color
});

chrome.browserAction.setBadgeText({
chrome.action.setBadgeText({
text: text
});
}