Guest User

Untitled

a guest
Jan 8th, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
  2. const keyString = isMac ? 'mac' : 'windows';
  3. const initialStorage = {
  4.   key: {
  5.     mac: 'Alt',
  6.     windows: 'Shift'
  7.   }
  8. };
  9.  
  10. let key = initialStorage.key;
  11. const textArr = [];
  12. let toastContainerEl;
  13. let toastEl;
  14. let toastItemEl;
  15. let pressedKey = false;
  16.  
  17. if (chrome.storage) {
  18.   chrome.storage.sync.get(data => {
  19.     if (Object.keys(data).length === 0) {
  20.       chrome.storage.sync.set(initialStorage);
  21.     } else {
  22.       key = data.key;
  23.     }
  24.   });
  25. }
  26.  
  27. window.addEventListener('load', () => {
  28.   init();
  29.  
  30.   if (chrome.storage) {
  31.     chrome.storage.onChanged.addListener(data => {
  32.       if (data.key) {
  33.         const { newValue, oldValue } = data.key;
  34.  
  35.         if (newValue[keyString] !== oldValue[keyString]) {
  36.           key = newValue;
  37.         }
  38.       }
  39.     });
  40.   }
  41. });
  42.  
  43. function init() {
  44.   toastContainerEl = document.createElement('div');
  45.   toastContainerEl.className = 'simple-copy-toast-container';
  46.   toastContainerEl.dataset.color = 'black';
  47.  
  48.   toastEl = document.createElement('div');
  49.   toastEl.className = 'toast';
  50.  
  51.   toastItemEl = document.createElement('div');
  52.   toastItemEl.className = 'toast-item';
  53.   //toastItemEl.innerHTML = 'copied!!';
  54.  
  55.   toastEl.appendChild(toastItemEl);
  56.   toastContainerEl.appendChild(toastEl);
  57.   document.body.appendChild(toastContainerEl);
  58.  
  59.   event();
  60. }
  61.  
  62. function event() {
  63.   window.addEventListener('keydown', e => {
  64.     if (key[keyString] === e.key) {
  65.       pressedKey = true;
  66.     }
  67.   });
  68.  
  69.   window.addEventListener('keyup', () => {
  70.     textArr.length = 0;
  71.     pressedKey = false;
  72.   });
  73.  
  74.   window.addEventListener('mousedown', e => {
  75.     const newPressedKey = e.getModifierState(key[keyString]);
  76.     if (!newPressedKey) {
  77.       textArr.length = 0;
  78.       pressedKey = false;
  79.     }
  80.  
  81.     if (pressedKey) {
  82.        
  83.       const selectionText = getSelection().toString();
  84.       let displayCopyTarget = true;
  85.  
  86.  
  87.       // this is when a text is selected
  88.       if (selectionText.trim().length > 0) {
  89.        
  90.         copy(selectionText);
  91.         displayCopyTarget = false;
  92.        
  93.         // Testing
  94.         // console.log('hi1');
  95.         toastItemEl.innerHTML = (selectionText); // this works
  96.        
  97.        
  98.       } else if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
  99.      
  100.         //console.log('hi2');
  101.         copy(e.target.value);
  102.         toastItemEl.innerHTML = (e.target.value);
  103.      
  104.        
  105.       } else {
  106.      
  107.         //console.log('hi3');
  108.         copy(e.target.textContent);
  109.        
  110.         toastItemEl.innerHTML = (e.target.textContent);
  111.        
  112.       }
  113.  
  114.       if (displayCopyTarget) {
  115.          
  116.         e.target.classList.add('copy-target');
  117.        
  118.         //toastItemEl.innerHTML = 'copiedssss!!';
  119.         //toastItemEl.innerHTML = selectionText;
  120.        
  121.         //console.log(selectionText);
  122.        
  123.        
  124.         setTimeout(() => {
  125.           e.target.classList.remove('copy-target');
  126.         }, 200);
  127.       }
  128.  
  129.       e.preventDefault();
  130.       e.stopPropagation();
  131.       e.cancelBubble = true;
  132.       return false;
  133.     }
  134.   });
  135. }
  136.  
  137. let timeout = null;
  138. function copy(message) {
  139.   textArr.push(message);
  140.  
  141.   navigator.clipboard.writeText(textArr.join('\n')).then(() => {
  142.     if (timeout) {
  143.       clearTimeout(timeout);
  144.       timeout = null;
  145.     } else {
  146.       toastItemEl.classList.add('enter');
  147.     }
  148.  
  149.     timeout = setTimeout(() => {
  150.       toastItemEl.classList.remove('enter');
  151.       timeout = null;
  152.     }, 1000);
  153.    
  154.   }, () => {
  155.     console.log('error');
  156.   })
  157. }
Advertisement
Add Comment
Please, Sign In to add comment