Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented lightning navigation #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
78 changes: 50 additions & 28 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const storageKey = "sfmWhySF";

{
const script = document.createElement("script");
script.src = chrome.runtime.getURL("lightning-navigation.js");
(document.head || document.documentElement).appendChild(script);
}

function init(setupTabUl) {
if (setupTabUl) {
let rows = [];
chrome.storage.sync.get([storageKey], function (items) {
let rowObj = items[storageKey] || [];
if (rowObj.length === 0) {
Expand All @@ -12,14 +17,10 @@ function init(setupTabUl) {

for (const rowId in rowObj) {
let row = rowObj[rowId];
rows.push(
setupTabUl.appendChild(
generateRowTemplate(row.tabTitle, row.url, row.openInNewTab)
);
}
setupTabUl.insertAdjacentHTML("beforeend", rows.join(""));

// Add click event listeners after rows are inserted
addClickEventListeners();
});
}
}
Expand Down Expand Up @@ -49,12 +50,31 @@ setTimeout(function () {
}, 3000);

function generateRowTemplate(tabTitle, url, openInNewTab) {
const li = document.createElement('li');
li.setAttribute('role', 'presentation');
li.className = 'oneConsoleTabItem tabItem slds-context-bar__item borderRight navexConsoleTabItem';
li.setAttribute('data-aura-class', 'navexConsoleTabItem');
li.setAttribute('data-url', url);

const target = openInNewTab ? "_blank" : "_self";
return `<li role="presentation" class="oneConsoleTabItem tabItem slds-context-bar__item borderRight navexConsoleTabItem" data-aura-class="navexConsoleTabItem" data-url="${url}">
<a role="tab" tabindex="-1" title="${tabTitle}" aria-selected="false" href="${url}" target="${target}" class="tabHeader slds-context-bar__label-action">
<span class="title slds-truncate">${tabTitle}</span>
</a>
</li>`;
const a = document.createElement('a');
a.setAttribute('role', 'tab');
a.setAttribute('tabindex', '-1');
a.setAttribute('title', tabTitle);
a.setAttribute('aria-selected', 'false');
a.setAttribute('href', url);
a.setAttribute('target', target);
a.classList.add('tabHeader','slds-context-bar__label-action');
// add click event listener on creation
a.addEventListener("click", handleLightningLinkClick);
Astisme marked this conversation as resolved.
Show resolved Hide resolved

const span = document.createElement('span');
span.classList.add('title','slds-truncate');
span.textContent = tabTitle;

a.appendChild(span);
li.appendChild(a);
return li;
}

function initTabs() {
Expand All @@ -78,21 +98,23 @@ function initTabs() {
return tabs;
}

function addClickEventListeners() {
chrome.storage.sync.get([storageKey], function (items) {
const rowObj = items[storageKey] || [];
for (const rowId in rowObj) {
let tab = rowObj[rowId];
document
.querySelectorAll(`a[href="${tab.url}"]`)
.forEach((link) => {
link.addEventListener("click", function (event) {
if (tab.openInNewTab) {
event.preventDefault();
window.open(tab.url, "_blank");
}
});
});
}
});
/**
* Handles the redirection to another Salesforce page without requiring a full reload.
*
* @param {Event} e - the click event
*/
function handleLightningLinkClick(e) {
e.preventDefault(); // Prevent the default link behavior (href navigation)
const url = e.currentTarget.href;
const target = e.currentTarget.target;
if (target === "_blank") {
open(url, target);
} else {
postMessage({
what: "lightningNavigation",
navigationType: "url",
url,
fallbackURL: url,
}, "*");
}
}
38 changes: 38 additions & 0 deletions lightning-navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Listener for custom events from the content script
function doLightningNavigation(details) {
try {
switch (details.navigationType) {
case "recordId": {
const recordEvent = $A.get("e.force:navigateToSObject");
recordEvent.setParams({ recordId: details.recordId });
recordEvent.fire();
break;
}
case "url": {
const urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({ url: details.url });
urlEvent.fire();
break;
}
default: {
console.error("Invalid navigation type");
}
}
} catch (error) {
console.error(`Navigation failed: ${error.message}`);
if (details.fallbackURL) {
open(details.fallbackURL, "_top");
}
}
}

// listen to possible updates from tableDragHandler
addEventListener("message", (e) => {
if (e.source != window) {
return;
}
const what = e.data.what;
if (what === "lightningNavigation") {
doLightningNavigation(e.data);
}
});
11 changes: 11 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
"run_at": "document_end"
}
],
"web_accessible_resources": [
{
"resources": [
"lightning-navigation.js"
],
"matches": [
"*://*.my.salesforce-setup.com/*",
"*://*.lightning.force.com/*"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this to be the setup urls only.
lightning.force.com/lightning/setup/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it's allowed from the browsers.

]
}
],
"icons": {
"16": "images/whysf16.png",
"32": "images/whysf32.png",
Expand Down