Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
- const keyString = isMac ? 'mac' : 'windows';
- const initialStorage = {
- key: {
- mac: 'Alt',
- windows: 'Shift'
- }
- };
- let key = initialStorage.key;
- const textArr = [];
- let toastContainerEl;
- let toastEl;
- let toastItemEl;
- let pressedKey = false;
- if (chrome.storage) {
- chrome.storage.sync.get(data => {
- if (Object.keys(data).length === 0) {
- chrome.storage.sync.set(initialStorage);
- } else {
- key = data.key;
- }
- });
- }
- window.addEventListener('load', () => {
- init();
- if (chrome.storage) {
- chrome.storage.onChanged.addListener(data => {
- if (data.key) {
- const { newValue, oldValue } = data.key;
- if (newValue[keyString] !== oldValue[keyString]) {
- key = newValue;
- }
- }
- });
- }
- });
- function init() {
- toastContainerEl = document.createElement('div');
- toastContainerEl.className = 'simple-copy-toast-container';
- toastContainerEl.dataset.color = 'black';
- toastEl = document.createElement('div');
- toastEl.className = 'toast';
- toastItemEl = document.createElement('div');
- toastItemEl.className = 'toast-item';
- //toastItemEl.innerHTML = 'copied!!';
- toastEl.appendChild(toastItemEl);
- toastContainerEl.appendChild(toastEl);
- document.body.appendChild(toastContainerEl);
- event();
- }
- function event() {
- window.addEventListener('keydown', e => {
- if (key[keyString] === e.key) {
- pressedKey = true;
- }
- });
- window.addEventListener('keyup', () => {
- textArr.length = 0;
- pressedKey = false;
- });
- window.addEventListener('mousedown', e => {
- const newPressedKey = e.getModifierState(key[keyString]);
- if (!newPressedKey) {
- textArr.length = 0;
- pressedKey = false;
- }
- if (pressedKey) {
- const selectionText = getSelection().toString();
- let displayCopyTarget = true;
- // this is when a text is selected
- if (selectionText.trim().length > 0) {
- copy(selectionText);
- displayCopyTarget = false;
- // Testing
- // console.log('hi1');
- toastItemEl.innerHTML = (selectionText); // this works
- } else if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
- //console.log('hi2');
- copy(e.target.value);
- toastItemEl.innerHTML = (e.target.value);
- } else {
- //console.log('hi3');
- copy(e.target.textContent);
- toastItemEl.innerHTML = (e.target.textContent);
- }
- if (displayCopyTarget) {
- e.target.classList.add('copy-target');
- //toastItemEl.innerHTML = 'copiedssss!!';
- //toastItemEl.innerHTML = selectionText;
- //console.log(selectionText);
- setTimeout(() => {
- e.target.classList.remove('copy-target');
- }, 200);
- }
- e.preventDefault();
- e.stopPropagation();
- e.cancelBubble = true;
- return false;
- }
- });
- }
- let timeout = null;
- function copy(message) {
- textArr.push(message);
- navigator.clipboard.writeText(textArr.join('\n')).then(() => {
- if (timeout) {
- clearTimeout(timeout);
- timeout = null;
- } else {
- toastItemEl.classList.add('enter');
- }
- timeout = setTimeout(() => {
- toastItemEl.classList.remove('enter');
- timeout = null;
- }, 1000);
- }, () => {
- console.log('error');
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment