Skip to content

Commit

Permalink
Merge pull request #77 from htrinter/revert-65-feature/58-remove-dupl…
Browse files Browse the repository at this point in the history
…icate-links

Revert "Feature: Ignore duplicate URLs (optionally)"
  • Loading branch information
htrinter authored Nov 26, 2023
2 parents 3154626 + d23bd03 commit b8d3b10
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 107 deletions.
7 changes: 0 additions & 7 deletions src/browseraction.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
>will open <span id="tabcount-number">0</span> new
<span id="tabcount-tab-label">tabs</span></span
>
<span id="tabcount-ignored">
(<span id="tabcount-ignored-number">0</span> ignored)
</span>
</abbr>
</span>
</section>
Expand All @@ -49,10 +46,6 @@
><input type="checkbox" id="reverse" tabindex="4" /> Load in reverse
order</label
>
<label class="checkbox"
><input type="checkbox" id="ignoreDuplicates" tabindex="5" /> Ignore duplicate URLs
</label
>
</section>
<section>
<label class="checkbox"
Expand Down
22 changes: 1 addition & 21 deletions src/browseraction/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ describe('test browser action', () => {
expect(uiDef.lazyLoadCheckbox).toBeTruthy();
expect(uiDef.randomCheckbox).toBeTruthy();
expect(uiDef.reverseCheckbox).toBeTruthy();
expect(uiDef.ignoreDuplicatesCheckbox).toBeTruthy();
expect(uiDef.preserveCheckbox).toBeTruthy();
expect(uiDef.openButton).toBeTruthy();
expect(uiDef.extractButton).toBeTruthy();
Expand All @@ -62,7 +61,6 @@ describe('test browser action', () => {
expect(uiDef.lazyLoadCheckbox.checked).toBe(false);
expect(uiDef.randomCheckbox.checked).toBe(false);
expect(uiDef.reverseCheckbox.checked).toBe(false);
expect(uiDef.ignoreDuplicatesCheckbox.checked).toBe(false);
expect(uiDef.preserveCheckbox.checked).toBe(false);
});

Expand All @@ -75,15 +73,13 @@ describe('test browser action', () => {
uiDef.lazyLoadCheckbox.click();
uiDef.randomCheckbox.click();
uiDef.reverseCheckbox.click();
uiDef.ignoreDuplicatesCheckbox.click();
uiDef.preserveCheckbox.click();

uiDef = getUIDef();
expect(uiDef.txtArea.value).toBe('foobar');
expect(uiDef.lazyLoadCheckbox.checked).toBe(true);
expect(uiDef.randomCheckbox.checked).toBe(true);
expect(uiDef.reverseCheckbox.checked).toBe(true);
expect(uiDef.ignoreDuplicatesCheckbox.checked).toBe(true);
expect(uiDef.preserveCheckbox.checked).toBe(true);

document.body.innerHTML = BODY_HTML;
Expand All @@ -93,7 +89,6 @@ describe('test browser action', () => {
expect(uiDef.lazyLoadCheckbox.checked).toBe(false);
expect(uiDef.randomCheckbox.checked).toBe(false);
expect(uiDef.reverseCheckbox.checked).toBe(false);
expect(uiDef.ignoreDuplicatesCheckbox.checked).toBe(false);
expect(uiDef.preserveCheckbox.checked).toBe(false);

await init();
Expand All @@ -102,8 +97,7 @@ describe('test browser action', () => {
expect(uiDef.txtArea.value).toBe('foobar');
expect(uiDef.lazyLoadCheckbox.checked).toBe(true);
expect(uiDef.randomCheckbox.checked).toBe(true);
expect(uiDef.reverseCheckbox.checked).toBe(true);
expect(uiDef.ignoreDuplicatesCheckbox.checked).toBe(true);
expect(uiDef.randomCheckbox.checked).toBe(true);
expect(uiDef.preserveCheckbox.checked).toBe(true);
});

Expand All @@ -116,7 +110,6 @@ describe('test browser action', () => {
expect(uiDef.txtArea.value).toBe('https://test.de');
expect(uiDef.lazyLoadCheckbox.checked).toBe(false);
expect(uiDef.randomCheckbox.checked).toBe(false);
expect(uiDef.ignoreDuplicatesCheckbox.checked).toBe(false);
expect(uiDef.preserveCheckbox.checked).toBe(true);
});

Expand Down Expand Up @@ -151,31 +144,26 @@ describe('test browser action', () => {
expect(options.txt).toBe('');
expect(options.lazyload).toBe(false);
expect(options.random).toBe(false);
expect(options.ignoreDuplicates).toBe(false);
expect(options.preserve).toBe(false);

uiDef.lazyLoadCheckbox.click();
uiDef.randomCheckbox.click();
uiDef.ignoreDuplicatesCheckbox.click();
uiDef.preserveCheckbox.click();

options = await getStoredOptions();
expect(options.txt).toBe('');
expect(options.lazyload).toBe(true);
expect(options.random).toBe(true);
expect(options.ignoreDuplicates).toBe(true);
expect(options.preserve).toBe(true);

uiDef.lazyLoadCheckbox.click();
uiDef.randomCheckbox.click();
uiDef.ignoreDuplicatesCheckbox.click();
uiDef.preserveCheckbox.click();

options = await getStoredOptions();
expect(options.txt).toBe('');
expect(options.lazyload).toBe(false);
expect(options.random).toBe(false);
expect(options.ignoreDuplicates).toBe(false);
expect(options.preserve).toBe(false);
});

Expand Down Expand Up @@ -241,13 +229,5 @@ describe('test browser action', () => {
expect(hasTabCount('3')).toBeTruthy();
await sleep(UPDATE_TAB_COUNT_DEBOUNCE_TIME_MS);
expect(hasTabCount('> 5000')).toBeTruthy();

uiDef.txtArea.value =
'https://test.de\n'.repeat(123).concat('\n\nhttps://spiegel.de\n \nhttps://zeit.de\n\n \n ');
uiDef.txtArea.dispatchEvent(new Event('input'));
expect(hasTabCount('> 5000')).toBeTruthy();
uiDef.ignoreDuplicatesCheckbox.checked = true;
await sleep(UPDATE_TAB_COUNT_DEBOUNCE_TIME_MS);
expect(hasTabCount('3')).toBeTruthy();
});
});
27 changes: 3 additions & 24 deletions src/browseraction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,11 @@ const debouncedSaveUrlList = debounce(

const updateTabCount = (ui: UIDef) => {
let tabCount = '0';
let tabCountIgnored = '0';
if (ui.txtArea.value) {
let lines = ui.txtArea.value
.split(URL_LINE_SPLIT_REGEX)
.filter((line) => line.trim() !== '')
.map((line) => line.trim());
if (ui.ignoreDuplicatesCheckbox.checked) {
const tabCountOriginal = lines.length;
lines = lines.reduce((uniqueLines, line) => uniqueLines.includes(line) ? uniqueLines : [...uniqueLines, line], []);
tabCountIgnored = String(tabCountOriginal - lines.length);
}
const lines = ui.txtArea.value.split(URL_LINE_SPLIT_REGEX);
if (lines.length <= 5000) {
// limit for performance reasons
tabCount = String(lines.length);
tabCount = String(lines.filter((line) => line.trim() !== '').length);
} else {
tabCount = '> 5000';
}
Expand All @@ -43,9 +34,6 @@ const updateTabCount = (ui: UIDef) => {
ui.tabCountNumber.textContent = tabCount;
ui.tabCountTabLabel.textContent = tabCount === '1' ? 'tab' : 'tabs';
ui.tabCount.style.visibility = tabCount === '0' ? 'hidden' : 'visible';

ui.tabCountIgnoredNumber.textContent = tabCountIgnored;
ui.tabCountIgnored.style.visibility = tabCountIgnored === '0' ? 'hidden' : 'visible';
};
const debouncedUpdateTabCount = debounce(
updateTabCount,
Expand All @@ -61,7 +49,6 @@ export const init = async (): Promise<void> => {
ui.lazyLoadCheckbox.checked = options.lazyload;
ui.randomCheckbox.checked = options.random;
ui.reverseCheckbox.checked = options.reverse;
ui.ignoreDuplicatesCheckbox.checked = options.ignoreDuplicates;
ui.preserveCheckbox.checked = options.preserve;

// add text input events
Expand All @@ -77,8 +64,7 @@ export const init = async (): Promise<void> => {
ui.txtArea.value,
ui.lazyLoadCheckbox.checked,
ui.randomCheckbox.checked,
ui.reverseCheckbox.checked,
ui.ignoreDuplicatesCheckbox.checked,
ui.reverseCheckbox.checked
);
});
ui.extractButton.addEventListener('click', () => {
Expand Down Expand Up @@ -106,13 +92,6 @@ export const init = async (): Promise<void> => {
(<HTMLInputElement>event.target).checked
)
);
ui.ignoreDuplicatesCheckbox.addEventListener('change', (event) => {
debouncedUpdateTabCount(ui);
storeValue<boolean>(
StorageKey.ignoreDuplicates,
(<HTMLInputElement>event.target).checked
);
});
ui.preserveCheckbox.addEventListener('change', (event) => {
const isChecked = (<HTMLInputElement>event.target).checked;
storeValue<boolean>(StorageKey.preserve, isChecked);
Expand Down
44 changes: 7 additions & 37 deletions src/browseraction/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('load tabs', () => {
});

test('load tabs in sequence', async () => {
loadSites(urlList, false, false, false, false);
loadSites(urlList, false, false, false);

expect(browser.tabs.create).toHaveBeenNthCalledWith(1, {
url: url1,
Expand All @@ -29,7 +29,7 @@ describe('load tabs', () => {
});

test('lazyload tabs in sequence', async () => {
loadSites(urlList, true, false, false, false);
loadSites(urlList, true, false, false);

expect(browser.tabs.create).toHaveBeenNthCalledWith(1, {
url: 'lazyloading.html#' + url1,
Expand All @@ -42,7 +42,7 @@ describe('load tabs', () => {
});

test('load tabs in random order', async () => {
loadSites(urlList, false, true, false, false);
loadSites(urlList, false, true, false);

expect(browser.tabs.create).toHaveBeenCalledWith({
url: url1,
Expand All @@ -55,7 +55,7 @@ describe('load tabs', () => {
});

test('lazyload tabs in random order', async () => {
loadSites(urlList, true, true, false, false);
loadSites(urlList, true, true, false);

expect(browser.tabs.create).toHaveBeenCalledWith({
url: 'lazyloading.html#' + url1,
Expand All @@ -68,7 +68,7 @@ describe('load tabs', () => {
});

test('load tabs in reverse order', async () => {
loadSites(urlList, false, false, true, false);
loadSites(urlList, false, false, true);

expect(browser.tabs.create).toHaveBeenCalledWith({
url: url2,
Expand All @@ -81,7 +81,7 @@ describe('load tabs', () => {
});

test('lazyload tabs in reverse order', async () => {
loadSites(urlList, true, false, true, false);
loadSites(urlList, true, false, true);

expect(browser.tabs.create).toHaveBeenCalledWith({
url: 'lazyloading.html#' + url2,
Expand All @@ -93,36 +93,8 @@ describe('load tabs', () => {
});
});

test('load tabs with duplicate URLs', async () => {
loadSites(urlList + '\n' + urlList, false, true, false, true);

expect(browser.tabs.create).toHaveBeenCalledTimes(2);
expect(browser.tabs.create).toHaveBeenCalledWith({
url: url1,
active: false,
});
expect(browser.tabs.create).toHaveBeenCalledWith({
url: url2,
active: false,
});
});

test('lazyload tabs with duplicate URLs', async () => {
loadSites(urlList + '\n' + urlList, true, true, false, true);

expect(browser.tabs.create).toHaveBeenCalledTimes(2);
expect(browser.tabs.create).toHaveBeenCalledWith({
url: 'lazyloading.html#' + url1,
active: false,
});
expect(browser.tabs.create).toHaveBeenCalledWith({
url: 'lazyloading.html#' + url2,
active: false,
});
});

test('prepend http protocol if no protocol given', async () => {
loadSites('test.de', false, false, false, false);
loadSites('test.de', false, false, false);

expect(browser.tabs.create).toHaveBeenNthCalledWith(1, {
url: 'http://test.de',
Expand All @@ -135,7 +107,6 @@ describe('load tabs', () => {
'http://test.de/f bar\n \n' + url2 + '\n\n \t \n ',
false,
false,
false,
false
);

Expand All @@ -155,7 +126,6 @@ describe('load tabs', () => {
'test.de\nhttp://test.de\nhttps://test.de\nfile://test.de\nview-source://test.de',
false,
false,
false,
false
);

Expand Down
7 changes: 1 addition & 6 deletions src/browseraction/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export function loadSites(
text: string,
lazyloading: boolean,
random: boolean,
reverse: boolean,
ignoreDuplicates: boolean
reverse: boolean
): void {
const urlschemes = ['http', 'https', 'file', 'view-source'];
let urls = text.split(URL_LINE_SPLIT_REGEX);
Expand All @@ -40,10 +39,6 @@ export function loadSites(
urls = shuffle(urls);
}

if (ignoreDuplicates) {
urls = urls.reduce((uniqueUrls, url) => uniqueUrls.includes(url) ? uniqueUrls : [...uniqueUrls, url], []);
}

for (let i = 0; i < urls.length; i++) {
let theurl = urls[i].trim();
if (theurl !== '') {
Expand Down
4 changes: 0 additions & 4 deletions src/browseraction/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export enum StorageKey {
lazyload = 'lazyload',
random = 'random',
reverse = 'reverse',
ignoreDuplicates = 'ignoreDuplicates',
preserve = 'preserve',
}

Expand All @@ -14,7 +13,6 @@ export interface StoredOptions {
[StorageKey.lazyload]: boolean;
[StorageKey.random]: boolean;
[StorageKey.reverse]: boolean;
[StorageKey.ignoreDuplicates]: boolean;
[StorageKey.preserve]: boolean;
}

Expand All @@ -23,15 +21,13 @@ export async function getStoredOptions(): Promise<StoredOptions> {
const lazyloadVal = await browser.storage.local.get(StorageKey.lazyload);
const randomVal = await browser.storage.local.get(StorageKey.random);
const reverseVal = await browser.storage.local.get(StorageKey.reverse);
const ignoreDuplicatesVal = await browser.storage.local.get(StorageKey.ignoreDuplicates);
const preserveVal = await browser.storage.local.get(StorageKey.preserve);

return {
txt: txtVal?.txt || '',
lazyload: lazyloadVal?.lazyload || false,
random: randomVal?.random || false,
reverse: reverseVal?.reverse || false,
ignoreDuplicates: ignoreDuplicatesVal?.ignoreDuplicates || false,
preserve: txtVal?.txt || preserveVal?.preserve || false,
};
}
Expand Down
14 changes: 6 additions & 8 deletions src/browseraction/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ export interface UIDef {
randomCheckbox: HTMLInputElement;
reverseCheckbox: HTMLInputElement;
preserveCheckbox: HTMLInputElement;
ignoreDuplicatesCheckbox: HTMLInputElement;
openButton: HTMLInputElement;
extractButton: HTMLInputElement;
tabCount: HTMLSpanElement;
tabCountNumber: HTMLSpanElement;
tabCountTabLabel: HTMLSpanElement;
tabCountIgnored: HTMLSpanElement;
tabCountIgnoredNumber: HTMLSpanElement;
}

export function getUIDef(): UIDef {
Expand All @@ -21,13 +18,14 @@ export function getUIDef(): UIDef {
randomCheckbox: document.getElementById('random') as HTMLInputElement,
reverseCheckbox: document.getElementById('reverse') as HTMLInputElement,
preserveCheckbox: document.getElementById('preserve') as HTMLInputElement,
ignoreDuplicatesCheckbox: document.getElementById('ignoreDuplicates') as HTMLInputElement,
openButton: document.getElementById('open') as HTMLInputElement,
extractButton: document.getElementById('extract') as HTMLInputElement,
tabCount: document.getElementById('tabcount') as HTMLSpanElement,
tabCountNumber: document.getElementById('tabcount-number') as HTMLSpanElement,
tabCountTabLabel: document.getElementById('tabcount-tab-label') as HTMLSpanElement,
tabCountIgnored: document.getElementById('tabcount-ignored') as HTMLSpanElement,
tabCountIgnoredNumber: document.getElementById('tabcount-ignored-number') as HTMLSpanElement
tabCountNumber: document.getElementById(
'tabcount-number'
) as HTMLSpanElement,
tabCountTabLabel: document.getElementById(
'tabcount-tab-label'
) as HTMLSpanElement,
};
}

0 comments on commit b8d3b10

Please sign in to comment.