Skip to content

Instantly share code, notes, and snippets.

@rahulbansal16
Created December 12, 2024 05:29
Show Gist options
  • Save rahulbansal16/0c45d2ef2930a63dd06c011866ff1282 to your computer and use it in GitHub Desktop.
Save rahulbansal16/0c45d2ef2930a63dd06c011866ff1282 to your computer and use it in GitHub Desktop.
Extension to copy the text from the screen
// ==UserScript==
// @name Enhanced Page Summary (Revised) 1.0
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Capture existing and new text content from the DOM and copy to clipboard
// @author You
// @match *://*/*
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
const TextCollector = {
textSet: new Set(),
isVisible: function(element) {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
},
collectTextFromNode: function(node) {
if (node.nodeType === Node.TEXT_NODE && node.parentElement && this.isVisibale(node.parentElement)) {
const text = node.textContent.trim();
if (text) this.textSet.add(text);
} else if (node.nodeType === Node.ELEMENT_NODE && node.tagName !== 'SCRIPT' && node.tagName !== 'STYLE') {
if (this.isVisible(node)) {
node.childNodes.forEach(child => this.collectTextFromNode(child));
}
}
},
collectAllText: function() {
this.collectTextFromNode(document.body);
},
getCollectedText: function() {
return Array.from(this.textSet).join('\n');
},
clear: function() {
this.textSet.clear();
}
};
const DOMObserver = {
observer: null,
observing: false,
init: function() {
this.observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => TextCollector.collectTextFromNode(node));
}
});
});
},
start: function() {
if (!this.observing) {
this.observer.observe(document.body, { childList: true, subtree: true });
this.observing = true;
}
},
stop: function() {
if (this.observing) {
this.observer.disconnect();
this.observing = false;
}
}
};
const UIManager = {
button: null,
createButton: function() {
const shadowHost = document.createElement('div');
shadowHost.attachShadow({ mode: 'open' });
shadowHost.style.cssText = 'position: fixed; right: 20px; top: 50%; z-index: 10000;';
document.body.appendChild(shadowHost);
this.button = document.createElement('button');
this.button.textContent = 'Start Listening';
this.button.style.cssText = `
all: initial;
font-family: Arial, sans-serif;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
`;
shadowHost.shadowRoot.appendChild(this.button);
},
setButtonState: function(isListening) {
this.button.textContent = isListening ? 'Stop Listening' : 'Start Listening';
}
};
const App = {
init: function() {
DOMObserver.init();
UIManager.createButton();
this.attachEventListeners();
},
attachEventListeners: function() {
UIManager.button.onclick = this.toggleListening.bind(this);
},
toggleListening: function() {
if (!DOMObserver.observing) {
TextCollector.clear();
TextCollector.collectAllText(); // Collect existing text
DOMObserver.start();
UIManager.setButtonState(true);
this.copyToClipboard("Existing text copied to clipboard. Now listening for changes...");
} else {
DOMObserver.stop();
UIManager.setButtonState(false);
this.copyToClipboard("All collected text copied to clipboard.");
}
},
copyToClipboard: function(message) {
const allText = TextCollector.getCollectedText();
GM_setClipboard(allText);
alert(message);
}
};
App.init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment