Skip to content

Commit 9f59076

Browse files
committed
[fix]: Handle open-show-all-data in background.js to prevent duplicate tabs
Move shortcut handling out of popup.js (where it fired on every open Salesforce tab due to chrome.runtime.sendMessage broadcast) into background.js. Uses chrome.tabs.query to get the active tab and the existing cookie-based sfHost resolution, then opens inspect.html directly via chrome.tabs.create — no broadcast involved.
1 parent c622647 commit 9f59076

2 files changed

Lines changed: 52 additions & 21 deletions

File tree

addon/background.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ chrome.commands?.onCommand.addListener((command) => {
8181
url: `https:///${sfHost}${link}`
8282
});
8383

84+
} else if (command === "open-show-all-data") {
85+
chrome.tabs.query({active: true, currentWindow: true}, tabs => {
86+
let tab = tabs[0];
87+
if (!tab?.url) return;
88+
let tabUrl = tab.url;
89+
let currentDomain;
90+
try { currentDomain = new URL(tabUrl).hostname; } catch { return; }
91+
chrome.cookies.get({url: tabUrl, name: "sid", storeId: tab.cookieStoreId}, cookie => {
92+
if (!cookie || currentDomain.endsWith(".mcas.ms")) {
93+
openShowAllData(currentDomain, tabUrl);
94+
return;
95+
}
96+
const [orgId] = cookie.value.split("!");
97+
const orderedDomains = ["salesforce.com", "cloudforce.com", "salesforce.mil", "cloudforce.mil", "sfcrmproducts.cn", "force.com"];
98+
let resolved = false;
99+
orderedDomains.forEach(domain => {
100+
chrome.cookies.getAll({name: "sid", domain, secure: true, storeId: tab.cookieStoreId}, cookies => {
101+
if (resolved) return;
102+
let sessionCookie = cookies.find(c => c.value.startsWith(orgId + "!") && c.domain != "help.salesforce.com");
103+
if (sessionCookie) {
104+
resolved = true;
105+
openShowAllData(sessionCookie.domain, tabUrl);
106+
}
107+
});
108+
});
109+
});
110+
});
84111
} else if (command.startsWith("open-")){
85112
chrome.runtime.sendMessage({
86113
msg: "shortcut_pressed", command, sfHost
@@ -118,4 +145,28 @@ async function clearSobjectsListCache() {
118145
console.error("Error clearing sobjectsList cache on update:", e);
119146
}
120147
}
148+
function openShowAllData(host, tabUrl) {
149+
let url;
150+
try { url = new URL(tabUrl); } catch { return; }
151+
let sobject = null;
152+
let sobjectMatch = url.pathname.match(/\/lightning\/[ro]\/([a-zA-Z0-9_]+)\/[a-zA-Z0-9]+/);
153+
if (sobjectMatch) sobject = sobjectMatch[1];
154+
let recordId = null;
155+
if (url.pathname.includes("/builder_platform_interaction/flowBuilder.app")) {
156+
let flowId = url.searchParams.get("flowId");
157+
if (flowId?.startsWith("301")) recordId = flowId;
158+
} else {
159+
for (let p of url.pathname.split("/")) {
160+
if (p.match(/^([a-zA-Z0-9]{3}|[a-zA-Z0-9]{15}|[a-zA-Z0-9]{18})$/) && p.includes("0000")) {
161+
recordId = p;
162+
break;
163+
}
164+
}
165+
}
166+
let args = new URLSearchParams();
167+
args.set("host", host);
168+
if (sobject) args.set("objectType", sobject);
169+
if (recordId) args.set("recordId", recordId);
170+
chrome.tabs.create({url: chrome.runtime.getURL("inspect.html?" + args)});
171+
}
121172
chrome.runtime.setUninstallURL("https://forms.gle/y7LbTNsFqEqSrtyc6");

addon/popup.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ if (typeof browser === "undefined") {
3636
if (request.msg === "shortcut_pressed") {
3737
if (request.command === "open-popup") {
3838
parent.postMessage({insextOpenPopup: true}, "*");
39-
} else if (request.command !== "open-show-all-data") {
39+
} else {
4040
parent.postMessage({command: request.command}, "*");
4141
}
4242
} else if (request.message === "tokenUpdated" && request.sfHost) {
@@ -138,7 +138,6 @@ class App extends React.PureComponent {
138138
};
139139
this.onContextUrlMessage = this.onContextUrlMessage.bind(this);
140140
this.onShortcutKey = this.onShortcutKey.bind(this);
141-
this.onShowAllDataShortcut = this.onShowAllDataShortcut.bind(this);
142141
this.onChangeApi = this.onChangeApi.bind(this);
143142
this.onContextRecordChange = this.onContextRecordChange.bind(this);
144143
this.updateReleaseNotesViewed = this.updateReleaseNotesViewed.bind(this);
@@ -326,31 +325,12 @@ class App extends React.PureComponent {
326325
let {sfHost} = this.props;
327326
addEventListener("message", this.onContextUrlMessage);
328327
addEventListener("keydown", this.onShortcutKey);
329-
chrome.runtime.onMessage.addListener(this.onShowAllDataShortcut);
330328
parent.postMessage({insextLoaded: true}, "*");
331329
setOrgInfo(this.props.sfHost);
332330
}
333331
componentWillUnmount() {
334332
removeEventListener("message", this.onContextUrlMessage);
335333
removeEventListener("keydown", this.onShortcutKey);
336-
chrome.runtime.onMessage.removeListener(this.onShowAllDataShortcut);
337-
}
338-
onShowAllDataShortcut(request) {
339-
if (request.msg === "shortcut_pressed" && request.command === "open-show-all-data") {
340-
let {sfHost} = this.props;
341-
let {contextUrl} = this.state;
342-
let recordId = contextUrl && getRecordId(contextUrl);
343-
let sobject = contextUrl && getSobject(contextUrl);
344-
let args = new URLSearchParams();
345-
args.set("host", sfHost);
346-
if (sobject) {
347-
args.set("objectType", sobject);
348-
}
349-
if (recordId) {
350-
args.set("recordId", recordId);
351-
}
352-
window.open(chrome.runtime.getURL("inspect.html?" + args), "_blank");
353-
}
354334
}
355335
isMac() {
356336
return navigator.userAgentData?.platform.toLowerCase().indexOf("mac") > -1 || navigator.userAgent.toLowerCase().indexOf("mac") > -1;

0 commit comments

Comments
 (0)