-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6266369
commit 66657bb
Showing
3 changed files
with
103 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import clipboardTextSvg from '../assets/clipboard.svg' | ||
import clipboardMdSvg from '../assets/clipboard2.svg' | ||
import checkmarkSvg from '../assets/checkmark.svg' | ||
|
||
export function injectOrUpdateButton(config, formatPreference, selectedStyle, siteConfigs) { | ||
let button = document.getElementById(config.buttonId) | ||
|
||
if (!button) { | ||
button = document.createElement('button') | ||
button.id = config.buttonId | ||
document.body.appendChild(button) | ||
} | ||
|
||
let icon = formatPreference === 'markdown' ? clipboardMdSvg : clipboardTextSvg | ||
|
||
button.className = `copy-button ${selectedStyle}` | ||
button.innerHTML = `<img src="${chrome.runtime.getURL(icon)}" alt="Copy Info">` | ||
|
||
button.addEventListener('click', () => handleButtonClick(config, formatPreference, siteConfigs, button)) | ||
} | ||
|
||
function handleButtonClick(config, formatPreference, siteConfigs, button) { | ||
const siteConfig = siteConfigs[config.siteKey] | ||
const pageConfig = siteConfig.pages[config.pageKey] | ||
const info = pageConfig.getInfo() | ||
const currentUrl = window.location.href | ||
|
||
let textToCopy = | ||
formatPreference === 'markdown' ? `${config.prefix} ${pageConfig.buildMarkdown(info, currentUrl)}` : `${config.prefix} ${pageConfig.buildPlaintext(info, currentUrl)}` | ||
|
||
navigator.clipboard | ||
.writeText(textToCopy) | ||
.then(() => { | ||
console.debug('Copied to clipboard: ' + textToCopy) | ||
updateButtonAfterCopy(button) | ||
}) | ||
.catch((err) => { | ||
console.error('Failed to copy text: ', err) | ||
}) | ||
} | ||
|
||
function updateButtonAfterCopy(button) { | ||
const originalHTML = button.innerHTML | ||
button.innerHTML = `<img src="${chrome.runtime.getURL(checkmarkSvg)}" alt="Copy Info">` | ||
button.classList.add('success') | ||
setTimeout(() => { | ||
button.innerHTML = originalHTML | ||
button.classList.remove('success') | ||
}, 2000) | ||
} | ||
|
||
export function removeButtons() { | ||
const buttons = document.querySelectorAll('.copy-button') | ||
buttons.forEach((button) => button.remove()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { injectOrUpdateButton, removeButtons } from './button.js' | ||
|
||
export function setupMessageListener(state, siteConfigs) { | ||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
switch (request.action) { | ||
case 'updateConfig': | ||
state.formatPreference = request.formatPreference | ||
state.selectedStyle = request.buttonStyle | ||
state.userSiteConfigs = request.siteConfigs | ||
state.currentConfig = request.config | ||
if (state.currentConfig) { | ||
injectOrUpdateButton(state.currentConfig, state.formatPreference, state.selectedStyle, siteConfigs) | ||
} else { | ||
removeButtons() | ||
} | ||
break | ||
case 'removeButtons': | ||
removeButtons() | ||
break | ||
case 'updatePreference': | ||
state.formatPreference = request.formatPreference | ||
break | ||
case 'updateStyle': | ||
state.selectedStyle = request.buttonStyle | ||
if (state.currentConfig) { | ||
injectOrUpdateButton(state.currentConfig, state.formatPreference, state.selectedStyle, siteConfigs) | ||
} | ||
break | ||
} | ||
}) | ||
} | ||
|
||
export function checkCurrentUrl() { | ||
chrome.runtime.sendMessage({ action: 'checkCurrentUrl' }) | ||
} |