Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfredo committed Feb 21, 2024
1 parent 40d72a9 commit 81482e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
39 changes: 22 additions & 17 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
//import { getStorage, setStorage } from "./popup";

const whyKey = 'sfmWhySF';
const setupTabUl = document.getElementsByClassName("tabBarItems slds-grid")[0];
let setupTabUl = document.getElementsByClassName("tabBarItems slds-grid")[0];
const setupLightning = "/lightning/setup/";
const setupDefaultPage = "/home";

function getStorage(callback){
chrome.storage.sync.get([whyKey], function(items) {
console.log(`get ${items}`);
callback(items);
});
}
Expand All @@ -20,7 +21,14 @@ function setStorage(tabs){
});
}

function generateRowTemplate(tabTitle, url){
function generateRowTemplate(row){
let { tabTitle, url } = row;
if(url.startsWith("/"))
url = url.slice(1);
if(url.endsWith("/"))
url = url.slice(0,url.length-1);
url = `${setupLightning}${url}${setupDefaultPage}`;

return `<li role="presentation" style="" class="oneConsoleTabItem tabItem slds-context-bar__item borderRight navexConsoleTabItem" data-aura-class="navexConsoleTabItem">
<a role="tab" tabindex="-1" title="${tabTitle}" aria-selected="false" href="${url}" class="tabHeader slds-context-bar__label-action" >
<span class="title slds-truncate">${tabTitle}</span>
Expand All @@ -30,38 +38,35 @@ function generateRowTemplate(tabTitle, url){

function initTabs(){
const tabs = [
{tabTitle : 'Flows', url: '/lightning/setup/Flows/home'},
{tabTitle : 'Users', url: '/lightning/setup/ManageUsers/home'}
{tabTitle : 'Flows', url: 'Flows/home'},
{tabTitle : 'Users', url: 'ManageUsers/home'}
]
setStorage(tabs);
return tabs;
}

function _init(items){
function init(items){
console.log(items);
//call inittabs if we did not find data inside storage
const rowObj = (items == null || items[whyKey] == null) ? initTabs() : items[whyKey];

const rows = [];
for (const row of rowObj) {
rows.push(generateRowTemplate(row.tabTitle,row.url))
rows.push(generateRowTemplate(row))
}
setupTabUl.insertAdjacentHTML('beforeend', rows.join(''));
}

function init(setupTabUl){
if (setupTabUl == null)
return;
getStorage(_init);
}

function delayLoadSetupTabs(count = 0) {

if (count > 5){
console.log('Why Salesforce - failed to find setup tab.');
console.error('Why Salesforce - failed to find setup tab.');
return;
}

setupTabUl == null ? setTimeout(function() { delayLoadSetupTabs(count++); }, 500) : init(setupTabUl);
if(setupTabUl == null){
setupTabUl = document.getElementsByClassName("tabBarItems slds-grid")[0];
setTimeout(function() { delayLoadSetupTabs(count + 1); }, 500);
} else getStorage(init);
}

setTimeout(function() { delayLoadSetupTabs(0); }, 3000);
delayLoadSetupTabs();
34 changes: 17 additions & 17 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const whyKey = "sfmWhySF";

function getStorage(callback){
chrome.storage.sync.get([whyKey], function(items) {
console.log(`get ${items}`);
callback(items);
});
}
Expand All @@ -22,7 +21,7 @@ function setStorage(tabs){

function deleteTab(){
this.closest(".tab").remove();
saveTab();
saveTabs();
}

function createElement(){
Expand All @@ -31,13 +30,18 @@ function createElement(){
return element;
}

function loadTemplateTab(){
tabAppendElement.append(createElement());
}

function loadTabs(items){
console.log(items);
if(items == null)
return;
if(items == null || items[whyKey] == null)
return loadTemplateTab();

const rowObjs = items[whyKey];
const elements = [];
for (const tab of rowObjs) {
for (const tab of rowObjs){
console.log(tab);
const element = createElement();
element.querySelector(".tabTitle").value = tab.tabTitle;
Expand All @@ -47,19 +51,15 @@ function loadTabs(items){
tabAppendElement.append(...elements);
}

function clearStorage(){
/*function clearStorage(){
chrome.storage.sync.remove([whyKey],function(){
const error = chrome.runtime.lastError;
if (error != null)
console.error(error);
})
}
}*/

function addTab(){
tabAppendElement.append(createElement());
}

function processTabs(){
function saveTabs(){
const tabs = [];
const tabElements = document.getElementsByClassName("tab");
Array.from(tabElements).forEach(function (tab) {
Expand All @@ -69,14 +69,14 @@ function processTabs(){
tabs.push({tabTitle, url});
}
});
return tabs;
setStorage(tabs);
}

function saveTab(){
const validTabs = processTabs();
setStorage(validTabs);
function addTab(){
tabAppendElement.append(createElement());
saveTabs()
}

saveButton.addEventListener("click", saveTab);
saveButton.addEventListener("click", saveTabs);
addButton.addEventListener("click", addTab);
getStorage(loadTabs);
2 changes: 1 addition & 1 deletion template-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"128": "images/whysf128.png"
},
"action": {
"default_title": "Why Salesforce",
"default_title": "Why Salesforce Reloaded",
"default_popup": "popup.html"
},
"minimum_chrome_version": "120.0.0",
Expand Down

0 comments on commit 81482e9

Please sign in to comment.