Discussions » Creation Requests

Make "write to clipboard" with text to instead console.log that text

§
Posted: 13-12-2023

Some sites like github uses JS to write text data to the clipboard for easy copying a block of text without the need of manually selecting the entire thing.

However, when scraping links off of a webpage, there are some cases that sites will not use <a href="https://example.com">...</a> that users rightclick and copy the link address, rather a button or it's site own context menu (like google drive) that execute JS to write the URL directly in the clipboard. I do not know any existing sites that still do this. Just in case for future sites.

So as a test, here is one: https://github.com/JustAnotherArchivist/snscrape and where it has:
pip3 install snscrape


There is a button to the right on that rectangular div that copies text onto your system's clipboard.

Is there a way to override the JS's clipboard writer to just instead console.log that text to be copied instead (without modifying the site's JS code)? Something like override the default functions, like here (monkey-patching, wikipedia) that you can modify BigInt's prototype so it can be JSON.stringify'ed.

§
Posted: 13-12-2023
Edited: 13-12-2023

pls copy and paste your answer into an AI before posting here, the solution might be 1 prompt away. I copy pasted your short essay and got a working answer.

Hope it helps!

"Yes, you can override the clipboard write function in JavaScript to instead log the text to the console. This can be done by creating a script that intercepts calls to the `writeText` method of the Clipboard API, which is commonly used to copy text to the clipboard.

Here's an example of how you might do this:

```javascript
navigator.clipboard.writeText = async function(data) {
console.log(data);
}
```

This code replaces the `writeText` method with a new function that logs the data to the console instead of writing it to the clipboard. You can run this code in the browser's console or include it in a userscript to apply it to specific pages.

Please note that this will only work for scripts that use the Clipboard API's `writeText` method. Some websites might use different methods to copy text to the clipboard, and those would not be affected by this code.

Also, keep in mind that this kind of modification can potentially break functionality on websites that rely on the ability to copy text to the clipboard. Use this responsibly and understand the potential impact before applying it.

Lastly, this approach might not work on all browsers as the Clipboard API is not supported everywhere. It's always a good idea to check the compatibility before using it."

§
Posted: 13-12-2023

Thanks. It worked on github. I did tested it on slack, and doesn't work. I did a little test by doing this:
document.getElementsByTagName("button")[0].click()
(replace the "0" with some other number to point to a "copy link" after you open the post's menu (the triple vertical dots))

and surprisingly, it doesn't error that the document isn't focused (this happens on github):
Uncaught (in promise) DOMException: Document is not focused.

But thankfully, slack URLs are exposed using the basic a tag.

NotYouMod
§
Posted: 13-12-2023

Just write this:

document.focus()
document.getElementsByTagName("button")[0].click()

To focus document, so you won't encounter that error.

Post reply

Đăng nhập để bình luận