-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into task/add-search-filter-menu
- Loading branch information
Showing
72 changed files
with
1,659 additions
and
700 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 97 additions & 103 deletions
200
packages/twenty-chrome-extension/src/contentScript/extractCompanyProfile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,117 @@ | ||
import createNewButton from '~/contentScript/createButton'; | ||
import { createDefaultButton } from '~/contentScript/createButton'; | ||
import extractCompanyLinkedinLink from '~/contentScript/utils/extractCompanyLinkedinLink'; | ||
import extractDomain from '~/contentScript/utils/extractDomain'; | ||
import { createCompany, fetchCompany } from '~/db/company.db'; | ||
import { CompanyInput } from '~/db/types/company.types'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
const insertButtonForCompany = async (): Promise<void> => { | ||
// Select the element in which to create the button. | ||
const parentDiv: HTMLDivElement | null = document.querySelector( | ||
'.org-top-card-primary-actions__inner', | ||
); | ||
export const checkIfCompanyExists = async () => { | ||
const { tab: activeTab } = await chrome.runtime.sendMessage({ | ||
action: 'getActiveTab', | ||
}); | ||
|
||
// Create the button with desired callback funciton to execute upon click. | ||
if (parentDiv) { | ||
// Extract company-specific data from the DOM | ||
const companyNameElement = document.querySelector( | ||
'.org-top-card-summary__title', | ||
); | ||
const domainNameElement = document.querySelector( | ||
'.org-top-card-primary-actions__inner a', | ||
); | ||
const addressElement = document.querySelectorAll( | ||
'.org-top-card-summary-info-list__info-item', | ||
)[1]; | ||
const employeesNumberElement = document.querySelectorAll( | ||
'.org-top-card-summary-info-list__info-item', | ||
)[3]; | ||
const companyURL = extractCompanyLinkedinLink(activeTab.url); | ||
|
||
// Get the text content or other necessary data from the DOM elements | ||
const companyName = companyNameElement | ||
? companyNameElement.getAttribute('title') | ||
: ''; | ||
const domainName = extractDomain( | ||
domainNameElement && domainNameElement.getAttribute('href'), | ||
); | ||
const address = addressElement | ||
? addressElement.textContent?.trim().replace(/\s+/g, ' ') | ||
: ''; | ||
const employees = employeesNumberElement | ||
? Number( | ||
employeesNumberElement.textContent | ||
?.trim() | ||
.replace(/\s+/g, ' ') | ||
.split('-')[0], | ||
) | ||
: 0; | ||
return await fetchCompany({ | ||
linkedinLink: { | ||
url: { eq: companyURL }, | ||
label: { eq: companyURL }, | ||
}, | ||
}); | ||
}; | ||
|
||
// Prepare company data to send to the backend | ||
const companyInputData: CompanyInput = { | ||
name: companyName ?? '', | ||
domainName: domainName, | ||
address: address ?? '', | ||
employees: employees, | ||
}; | ||
export const addCompany = async () => { | ||
// Extract company-specific data from the DOM | ||
const companyNameElement = document.querySelector( | ||
'.org-top-card-summary__title', | ||
); | ||
const domainNameElement = document.querySelector( | ||
'.org-top-card-primary-actions__inner a', | ||
); | ||
const addressElement = document.querySelectorAll( | ||
'.org-top-card-summary-info-list__info-item', | ||
)[1]; | ||
const employeesNumberElement = document.querySelectorAll( | ||
'.org-top-card-summary-info-list__info-item', | ||
)[3]; | ||
|
||
// Extract active tab url using chrome API - an event is triggered here and is caught by background script. | ||
const { url: activeTabUrl } = await chrome.runtime.sendMessage({ | ||
action: 'getActiveTabUrl', | ||
}); | ||
// Get the text content or other necessary data from the DOM elements | ||
const companyName = companyNameElement | ||
? companyNameElement.getAttribute('title') | ||
: ''; | ||
const domainName = extractDomain( | ||
domainNameElement && domainNameElement.getAttribute('href'), | ||
); | ||
const address = addressElement | ||
? addressElement.textContent?.trim().replace(/\s+/g, ' ') | ||
: ''; | ||
const employees = employeesNumberElement | ||
? Number( | ||
employeesNumberElement.textContent | ||
?.trim() | ||
.replace(/\s+/g, ' ') | ||
.split('-')[0], | ||
) | ||
: 0; | ||
|
||
// Convert URLs like https://www.linkedin.com/company/twenty/about/ to https://www.linkedin.com/company/twenty | ||
const companyURL = extractCompanyLinkedinLink(activeTabUrl); | ||
companyInputData.linkedinLink = { url: companyURL, label: companyURL }; | ||
// Prepare company data to send to the backend | ||
const companyInputData: CompanyInput = { | ||
name: companyName ?? '', | ||
domainName: domainName, | ||
address: address ?? '', | ||
employees: employees, | ||
}; | ||
|
||
const company = await fetchCompany({ | ||
linkedinLink: { | ||
url: { eq: companyURL }, | ||
label: { eq: companyURL }, | ||
}, | ||
}); | ||
if (company) { | ||
const savedCompany: HTMLDivElement = createNewButton( | ||
'Saved', | ||
async () => {}, | ||
); | ||
// Include the button in the DOM. | ||
parentDiv.prepend(savedCompany); | ||
// Extract active tab url using chrome API - an event is triggered here and is caught by background script. | ||
const { tab: activeTab } = await chrome.runtime.sendMessage({ | ||
action: 'getActiveTab', | ||
}); | ||
|
||
// Write button specific styles here - common ones can be found in createButton.ts. | ||
const buttonSpecificStyles = { | ||
alignSelf: 'end', | ||
}; | ||
// Convert URLs like https://www.linkedin.com/company/twenty/about/ to https://www.linkedin.com/company/twenty | ||
const companyURL = extractCompanyLinkedinLink(activeTab.url); | ||
companyInputData.linkedinLink = { url: companyURL, label: companyURL }; | ||
|
||
Object.assign(savedCompany.style, buttonSpecificStyles); | ||
} else { | ||
const newButtonCompany: HTMLDivElement = createNewButton( | ||
'Add to Twenty', | ||
async () => { | ||
const response = await createCompany(companyInputData); | ||
const company = await createCompany(companyInputData); | ||
return company; | ||
}; | ||
|
||
if (response) { | ||
newButtonCompany.textContent = 'Saved'; | ||
newButtonCompany.setAttribute('disabled', 'true'); | ||
export const insertButtonForCompany = async () => { | ||
const companyButtonDiv = createDefaultButton( | ||
'twenty-company-btn', | ||
async () => { | ||
if (isDefined(companyButtonDiv)) { | ||
const companyBtnSpan = companyButtonDiv.getElementsByTagName('span')[0]; | ||
companyBtnSpan.textContent = 'Saving...'; | ||
const company = await addCompany(); | ||
if (isDefined(company)) { | ||
companyBtnSpan.textContent = 'Saved'; | ||
Object.assign(companyButtonDiv.style, { pointerEvents: 'none' }); | ||
} else { | ||
companyBtnSpan.textContent = 'Try again'; | ||
} | ||
} | ||
}, | ||
); | ||
|
||
// Button specific styles once the button is unclickable after successfully sending data to server. | ||
newButtonCompany.addEventListener('mouseenter', () => { | ||
const hoverStyles = { | ||
backgroundColor: 'black', | ||
borderColor: 'black', | ||
cursor: 'default', | ||
}; | ||
Object.assign(newButtonCompany.style, hoverStyles); | ||
}); | ||
} else { | ||
newButtonCompany.textContent = 'Try Again'; | ||
} | ||
}, | ||
); | ||
const parentDiv: HTMLDivElement | null = document.querySelector( | ||
'.org-top-card-primary-actions__inner', | ||
); | ||
|
||
// Include the button in the DOM. | ||
parentDiv.prepend(newButtonCompany); | ||
if (isDefined(parentDiv)) { | ||
Object.assign(companyButtonDiv.style, { | ||
marginLeft: '.8rem', | ||
marginTop: '.4rem', | ||
}); | ||
parentDiv.prepend(companyButtonDiv); | ||
} | ||
|
||
// Write button specific styles here - common ones can be found in createButton.ts. | ||
const buttonSpecificStyles = { | ||
alignSelf: 'end', | ||
}; | ||
const companyBtnSpan = companyButtonDiv.getElementsByTagName('span')[0]; | ||
const company = await checkIfCompanyExists(); | ||
|
||
Object.assign(newButtonCompany.style, buttonSpecificStyles); | ||
} | ||
if (isDefined(company)) { | ||
companyBtnSpan.textContent = 'Saved'; | ||
Object.assign(companyButtonDiv.style, { pointerEvents: 'none' }); | ||
} else { | ||
companyBtnSpan.textContent = 'Add to Twenty'; | ||
} | ||
}; | ||
|
||
export default insertButtonForCompany; |
Oops, something went wrong.