Skip to content

Commit

Permalink
Anchor Links Option (#29)
Browse files Browse the repository at this point in the history
* Anchor Links Option

* Update Text

* Update Logging
  • Loading branch information
smashedr authored Jun 17, 2024
1 parent f3d1e08 commit 18e4b1e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ <h1>Open Links in New Tab</h1>
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="anchorLinks">
<label class="form-check-label" for="anchorLinks" aria-describedby="anchorLinksHelp">
Update Anchor Links
<span data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Open Same Origin Anchor Links in New Tab.">
<i class="fa-solid fa-circle-info ms-1"></i>
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="autoReload">
<label class="form-check-label" for="autoReload" aria-describedby="autoReloadHelp">
Expand Down
9 changes: 9 additions & 0 deletions src/html/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="anchorLinks">
<label class="form-check-label" for="anchorLinks" aria-describedby="anchorLinksHelp">
Update Anchor Links
<span data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Open Same Origin Anchor Links in New Tab.">
<i class="fa-solid fa-circle-info ms-1"></i>
</span>
</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="autoReload">
<label class="form-check-label" for="autoReload" aria-describedby="autoReloadHelp">
Expand Down
1 change: 1 addition & 0 deletions src/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function onInstalled(details) {
setDefaultOptions({
onScroll: false,
onAttributes: false,
anchorLinks: false,
autoReload: true,
updateAll: true,
noOpener: true,
Expand Down
25 changes: 12 additions & 13 deletions src/js/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

const contentScript = true
let tabEnabled = false
let noOpener = false
let options = {}

;(async () => {
const { options, sites } = await chrome.storage.sync.get([
'options',
'sites',
])
noOpener = options.noOpener
if (sites?.includes(window.location.host)) {
let data = await chrome.storage.sync.get(['options', 'sites'])
options = data.options
if (data.sites?.includes(window.location.host)) {
console.log(`Enabled Host: ${window.location.host}`)
await activateTab('green')
}
Expand Down Expand Up @@ -38,7 +35,6 @@ async function activateTab(color) {
console.info('Activating Tab...')
tabEnabled = true
updateLinks()
const { options } = await chrome.storage.sync.get(['options'])
const observer = new MutationObserver(updateLinks)
observer.observe(document.body, {
attributes: options.onAttributes,
Expand All @@ -62,9 +58,15 @@ function updateLinks() {
const elements = document.getElementsByTagName('a')
for (const element of elements) {
if (element.href !== '#') {
if (!options.anchorLinks && element.href.includes('#')) {
const url = new URL(element.href)
if (url.origin === window.location.origin) {
continue
}
}
if (element.target !== '_blank') {
element.target = '_blank'
if (noOpener) {
if (options.noOpener) {
element.setAttribute('rel', 'noopener')
}
}
Expand Down Expand Up @@ -92,7 +94,6 @@ async function onChanged(changes, namespace) {
}
} else if (tabEnabled) {
console.log(`Disabling: ${window.location.host}`)
const { options } = await chrome.storage.sync.get(['options'])
if (options.autoReload) {
window.location.reload()
} else {
Expand All @@ -103,9 +104,7 @@ async function onChanged(changes, namespace) {
}
}
} else if (namespace === 'sync' && key === 'options') {
if (oldValue.noOpener !== newValue.noOpener) {
noOpener = newValue.noOpener
}
options = newValue
}
}
}
Expand Down

0 comments on commit 18e4b1e

Please sign in to comment.