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

feat: async features #42

Merged
merged 3 commits into from
Feb 8, 2024
Merged
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
6 changes: 1 addition & 5 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]

# Environment variables available to all jobs and steps in this workflow
env:
CHROME_VERSION: 121.0.6167.85

jobs:
audit:
name: Audit dependencies
Expand Down Expand Up @@ -45,7 +41,7 @@ jobs:

test:
name: E2E Tests
runs-on: ubuntu-4-core-runner
runs-on: rows-fe-default
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
20 changes: 16 additions & 4 deletions e2e/scrappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,28 @@ describe('RowsX - scrappers tests', () => {

await appPage.bringToFront();
await appPage.goto(spec.url, { waitUntil: 'domcontentloaded' });

const client = await appPage.target().createCDPSession();
await client.send('Browser.setPermission', {
origin: new URL(spec.url),
permission: {
name: 'clipboard-write',
allowWithoutSanitization: true,
},
setting: 'granted',
});

await appPage.evaluate(() => navigator.clipboard.writeText(''));
await extensionPage.goto(extensionUrl, { waitUntil: 'domcontentloaded' });
await appPage.waitForTimeout(500);
await appPage.waitForTimeout(200);
await extensionPage.bringToFront();
const button = await extensionPage.waitForSelector('.copy-btn');
await button.click();
await extensionPage.waitForTimeout(180);
await appPage.bringToFront();

const context = await browser.defaultBrowserContext();
await context.overridePermissions(spec.url, ['clipboard-read']);
const clipboard = await appPage.evaluate(() => navigator.clipboard.readText());
const clipboard = await appPage.evaluate(async () => await navigator.clipboard.readText());


// close pages
await appPage.close();
Expand Down
4 changes: 1 addition & 3 deletions jest-puppeteer.config.cjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module.exports = {
launch: {
dumpio: false,
headless: 'new',
headless: 'new',
args: [
'--disable-extensions-except=./dist',
'--load-extension=./dist',
'--disable-gpu',
'--no-sandbox',
],
},
};
9 changes: 8 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ import './index.css';
import NoResults from './components/no-results';
import Header from './components/header';
import Preview from './components/preview';
import LoadingSkeleton from './components/loading-skeleton';

const App = () => {
const [loading, setLoading] = useState(true);
const [results, setResults] = useState([]);

useEffect(() => {
chrome.runtime.sendMessage({ action: 'rows-x:scrap' }, (response) => {
setResults(response);
setLoading(false);
});
}, []);

return (
<>
<Header />
<div className="container">
{results.length > 0 ? <Preview results={results} /> : <NoResults />}
{loading ? (
<LoadingSkeleton />
) : (
<>{results.length > 0 ? <Preview results={results} /> : <NoResults />}</>
)}
</div>
</>
);
Expand Down
62 changes: 62 additions & 0 deletions src/components/loading-skeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function randomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

const LoadingSkeleton = () => {
return (
<div className="skeleton-results">
<div className="table-preview" style={{ opacity: 0.5 }}>
<div className="table-header">
<caption
className="title skeleton"
style={{ background: 'var(--light-grey)', width: 150, height: 14 }}
/>
<div className="pill skeleton" style={{ height: 20, width: 60 }}></div>
</div>
<div className="table-body">
<div className="table-container">
<table>
{new Array(5).fill(0).map((_, index) => (
<tr key={index}>
<td>
<div
className="skeleton"
style={{
background: 'var(--light-grey)',
width: randomNumber(30, 80),
height: 14,
}}
/>
</td>
<td>
<div
className="skeleton"
style={{
background: 'var(--light-grey)',
width: randomNumber(30, 80),
height: 14,
}}
/>
</td>
<td>
<div
className="skeleton"
style={{
background: 'var(--light-grey)',
width: randomNumber(30, 80),
height: 14,
}}
/>
</td>
</tr>
))}
</table>
<div className="shade" />
</div>
</div>
</div>
</div>
);
};

export default LoadingSkeleton;
7 changes: 3 additions & 4 deletions src/components/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ const Preview: FunctionComponent<Props> = ({ results = [] }) => {
<Button
className="copy-btn"
type="secondary"
onClick={() => {
navigator.clipboard
.writeText(array2tsv(result.table))
.then(() => setTimeout(() => window.close(), 200));
onClick={async () => {
await navigator.clipboard.writeText(array2tsv(result.table));
setTimeout(() => window.close(), 200);
}}
>
<img alt="copy" src="/icons/copy.svg" />
Expand Down
14 changes: 14 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ a:link {
width: 22.5rem;
max-height: 600px;
}

.skeleton {
border-radius: 4px;
animation: skeleton-loading 1s linear infinite alternate;
}

@keyframes skeleton-loading {
0% {
background-color: var(--lighter-grey);
}
100% {
background-color: #c7c7c7;
}
}
4 changes: 4 additions & 0 deletions src/utils/scrappers/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export async function customScrapper(options: ScrapperOptions) {
);
});

if (tableElements.length <= 0) {
return [];
}

return [
{
title: options.header,
Expand Down
5 changes: 3 additions & 2 deletions src/utils/scrappers/html-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ export async function scrapHTMLTables() {
) as DOM_Element;

if (titleElement) {
title = titleElement?.innerText.replaceAll('\n', ' ').trim();
title = titleElement?.innerText?.replaceAll('\n', ' ')?.trim() ?? '';
}

scrapElement = scrapElement?.parentElement as DOM_Element;
}

titles.push(title.replace('[edit]', ''));
titles.push(title);

return Array.from(tableElement!.querySelectorAll(rowSelector)).map((tr) => {
if (!tr) {
Expand All @@ -108,6 +108,7 @@ export async function scrapHTMLTables() {
});
});

// Maps the table to the correct structure and after that remove the empty tables
return tables
.map((table, index: number) => ({
title: titles[index],
Expand Down
Loading