Skip to content

Commit 3a8c446

Browse files
authored
Fix Open URLs and Add Lazy Loading (#75)
* Fix Open URLs and Add Lazy Loading * Cleanup * Cleanup Logging * Make lazy Optional * Add Protocol Check to openURL * Change onFocus to Window * Add Comments * Fix Options... Wow...
1 parent df929df commit 3a8c446

11 files changed

Lines changed: 84 additions & 10 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Easily extract, parse, or open all links/domains from a site or text with optional filters.",
44
"homepage_url": "https://link-extractor.cssnr.com/",
55
"author": "Shane",
6-
"version": "0.6.0",
6+
"version": "0.6.1",
77
"manifest_version": 3,
88
"commands": {
99
"_execute_action": {

src/css/lazy.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* CSS for lazy.html */
2+
3+
body {
4+
background-color: #1c1b21;
5+
}

src/html/lazy.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title></title>
6+
<link rel="stylesheet" type="text/css" href="../css/lazy.css">
7+
</head>
8+
<body>
9+
10+
<script type="text/javascript" src="../js/lazy.js"></script>
11+
12+
</body>
13+
</html>

src/html/options.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ <h1>Link Extractor</h1>
5252
</div>
5353
</div>
5454

55+
<div class="form-check form-switch">
56+
<input class="form-check-input form-control" type="checkbox" role="switch" id="lazyLoad">
57+
<label class="form-check-label" for="lazyLoad" aria-describedby="lazyLoadHelp">
58+
Lazy Load Opened Tabs
59+
<span data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Don't Load Tabs Until Clicked On">
60+
<i class="fa-solid fa-circle-info ms-1"></i>
61+
</span>
62+
</label>
63+
</div>
5564
<div class="form-check form-switch">
5665
<input class="form-check-input form-control" type="checkbox" role="switch" id="removeDuplicates">
5766
<label class="form-check-label" for="removeDuplicates" aria-describedby="removeDuplicatesHelp">

src/html/popup.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@
4747
</div>
4848

4949
<form id="options-form">
50+
<div class="form-check form-switch">
51+
<input class="form-check-input form-control" type="checkbox" role="switch" id="lazyLoad">
52+
<label class="form-check-label" for="lazyLoad" aria-describedby="lazyLoadHelp">
53+
Lazy Load Opened Tabs
54+
<span data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Don't Load Tabs Until Clicked On">
55+
<i class="fa-solid fa-circle-info ms-1"></i>
56+
</span>
57+
</label>
58+
</div>
5059
<div class="form-check form-switch">
5160
<input class="form-check-input form-control" type="checkbox" role="switch" id="removeDuplicates">
5261
<label class="form-check-label" for="removeDuplicates" aria-describedby="removeDuplicatesHelp">

src/js/exports.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ export async function saveOptions(event) {
128128
}
129129
}
130130

131+
/**
132+
* Open URL
133+
* @function openURL
134+
* @param {String} url
135+
* @param {Boolean} [lazy]
136+
*/
137+
export function openURL(url, lazy = false) {
138+
// console.debug('openLink:', url, lazy)
139+
if (!url.includes('://')) {
140+
url = `http://${url}`
141+
}
142+
// console.debug('url:', url)
143+
if (lazy) {
144+
const lazyURL = new URL(chrome.runtime.getURL('/html/lazy.html'))
145+
lazyURL.searchParams.append('url', url)
146+
chrome.tabs.create({ active: false, url: lazyURL.href })
147+
} else {
148+
chrome.tabs.create({ active: false, url })
149+
}
150+
}
151+
131152
export function updateManifest() {
132153
const manifest = chrome.runtime.getManifest()
133154
document

src/js/lazy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// JS for lazy.html
2+
3+
const searchParams = new URLSearchParams(window.location.search)
4+
const url = new URL(searchParams.get('url'))
5+
6+
document.title = `${url.host}${url.pathname}`
7+
8+
const link = document.createElement('link')
9+
link.rel = 'icon'
10+
link.href = `${url.origin}/favicon.ico`
11+
document.head.appendChild(link)
12+
13+
window.addEventListener('focus', () => {
14+
console.log('url:', url)
15+
window.location = url.href
16+
})

src/js/links.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// JS for links.html
22

3-
import { textFileDownload } from './exports.js'
3+
import { openURL, textFileDownload } from './exports.js'
44

55
window.addEventListener('keydown', handleKeyboard)
66
document.addEventListener('DOMContentLoaded', initLinks)
@@ -314,14 +314,15 @@ function downloadFileClick(event) {
314314
* @function openLinksClick
315315
* @param {MouseEvent} event
316316
*/
317-
function openLinksClick(event) {
317+
async function openLinksClick(event) {
318318
console.debug('openLinksClick:', event)
319319
const closest = event.target?.closest('button')
320320
const links = getTableLinks(closest?.dataset?.target)
321321
// console.debug('links:', links)
322+
const { options } = await chrome.storage.sync.get(['options'])
322323
if (links) {
323324
links.split('\n').forEach(function (url) {
324-
chrome.tabs.create({ active: false, url }).then()
325+
openURL(url, options.lazyLoad)
325326
})
326327
} else {
327328
showToast('No Links to Open.', 'warning')

src/js/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ document
3131
.getElementById('options-form')
3232
.addEventListener('submit', (e) => e.preventDefault())
3333
document
34-
.querySelectorAll('#options-form > input, select')
34+
.querySelectorAll('#options-form input, select')
3535
.forEach((el) => el.addEventListener('change', saveOptions))
3636
document
3737
.querySelectorAll('[data-bs-toggle="tooltip"]')

src/js/popup.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {
44
checkPerms,
55
injectTab,
6+
openURL,
67
requestPerms,
78
saveOptions,
89
updateManifest,
@@ -139,6 +140,7 @@ async function linksForm(event) {
139140
event.preventDefault()
140141
const value = event.target.elements['links-text'].value
141142
// console.debug('value:', value)
143+
const { options } = await chrome.storage.sync.get(['options'])
142144
if (event.submitter.id === 'parse-links') {
143145
const urls = extractURLs(value)
144146
// console.debug('urls:', urls)
@@ -149,17 +151,14 @@ async function linksForm(event) {
149151
const urls = extractURLs(value)
150152
// console.debug('urls:', urls)
151153
urls.forEach(function (url) {
152-
chrome.tabs.create({ active: false, url })
154+
openURL(url.href, options.lazyLoad)
153155
})
154156
} else if (event.submitter.id === 'open-text') {
155157
let text = value.split(/\s+/).filter((s) => s !== '')
156158
// console.debug('text:', text)
157159
text.forEach(function (url) {
158160
// links without a : get prepended the web extension url by default
159-
if (!url.includes(':')) {
160-
url = `http://${url}`
161-
}
162-
chrome.tabs.create({ active: false, url })
161+
openURL(url, options.lazyLoad)
163162
})
164163
} else {
165164
console.error('Unknown event.submitter:', event.submitter)

0 commit comments

Comments
 (0)