How do I prevent "Read more: www.site.com" from being injected with non-Tynt JavaScript into copy-paste actions?

30

10

There are many sites that insert something like "Read more: www.site.com" into your clipboard when you copy text in your browser. I find this abominable. A google search reveals some methods to stop this if the website happens to be using the service Tynt (by just blocking the Tynt domain), but many websites use home-grown JavaScript.

Is there a general way to block this behavior besides just turning off JavaScript? I use Chrome but I am interested in all solutions.

EDIT: To be clear, I want to be able to keep the rest of the JavaScript functionality for these websites, since many will break without it.

EDIT #2: Here are two example websites which continue to harass me despite my use of a Tynt blocker: Marginal Revolution | The Fiscal Times

Here are two StackOverflow questions and two blog posts which explain ways to implement this shady practice manually without Tynt. Here is a blog post describing how hard this turns out to be to block reliably. Here is the most recent discussion I could find (March 2013) which offers suggestions on how to block this in Chrome using AdBlock, but it didn't work for me.

Jess Riedel

Posted 2014-01-19T02:10:24.293

Reputation: 558

Answers

24

The site adding the annoying "Read more" stuff is ShareThis.

To prevent this bad behavior, you have three different alternatives:

Disable the clipboard events

These websites are using the Clipboard APIs, which allows web developers to intercept the copy/cut/paste actions and execute some code when they are performed. This is how ShareThis (and other websites like that) works. It simply waits for the copy event and right before the effective copy is performed it adds an additional "layer" of text which contains the annoying "- See ...".

Now the question is: is there any kind of method to disable the clipboard events? Unfortunately, I have not been able to find a method to do this in Chrome/Chromium, but in Firefox it's possible in two different ways.

  • Go in about:config and search for dom.event.clipboardevents.enabled. Double click on the key (set it to false) and voila! You have disabled the clipboard events and no one will touch your clipboard again.
  • For older versions of Firefox (really, really older), there is this extension which does the exact same thing of the about:config option.

Disabling clipboard events should not damage the experience of any website since they are rarely used and there isn't really a purpose to use them (other than spamming).

Let's head over to the second solution.

Block ShareThis

If you don't need ShareThis, you can simply block the w.sharethis.com domain. The Javascript responsible for loading ShareThis (and registering the ClipboardEvent) is loaded from that website.

You may block it in different ways, ranging from a simple AdBlock filter to editing your hosts file (this is not covered nor linked here since I can't put more links due to my reputation).

An example of doing that via the hosts file:

127.0.0.1 w.sharethis.com

The third solution is the hardest one and it should be used only as a last resort.

Disable the selection feature on the problematic websites

To edit the content which is copied to the clipboard, these websites use the Selection API which allows them to edit selections on-the-fly. So, a solution is to completely disable any kind of Selection (on the code-side, obviously. You will still be able to perform selections).

This can be done with a simple Tampermonkey/Greasemonkey script. I tested it only on Firefox since I can't install Chrome right now. I'm sorry for that.

This is the source code:

// ==UserScript==
// @name        Goodbye selections
// @namespace   tag: utils
// @include     $put_here_a_website_you'd_like_to_disable_selections$
// @include     $more_websites$
// @version     1
// @grant       none
// ==/UserScript==
(function() {
    var disableSelections = function() {
        document.getSelection = window.getSelection = function() {
            return { isCollapsed: true };
        };
    };
    var script = document.createElement ("script");
    script.appendChild (document.createTextNode ("(" + disableSelections + ")();"));
    (document.body || document.head || document.documentElement).appendChild (script);
})();

To let this work, you should create a new Greasemonkey/Tampermonkey script and adjust the @include directives. You can put one website per line, and it has to be done like @include http://bad.website.address/.

I tested it with both the websites you have linked and it works with no problems. However, keep in mind that this may cause problems since Selections are used by perfectly legit websites (for example, StackExchange textboxes use them to insert a symbol, when you click on the button, to the position of your caret), so you should enable that userscript only on problematic websites.

(note that you may need to remove the lines starting with // if you are creating the userscript from the Greasemonkey/Tampermonkey menus, they will automatically add it)

The explanation of the userscript is pretty simple. First, it defines a function named disableSelections which replaces the default document.getSelection and window.getSelection functions with one that simply returns an object containing { isCollapsed: true }. Why? Because ShareThis (I checked in their JS code) calls that function and checks if the isCollapsed property is set to true (if it is, it stops the "clipboard poisoning"). Other websites like that maybe won't perform that check, but they will end up simply with an error when they try to call a legit function of the Selection object.

Then the function is injected in the body/header/document and it will be automatically executed. A question you may ask is: if Javascript allows to override (almost) every function, why didn't you override the addEventListener function to simply do nothing when the event is copy/cut/paste? The answer is pretty simple. An userscript is executed at a non-easily-predictable time, this means that the ShareThis Javascript can be loaded before the userscript, and it won't do anything. Instead, by just overriding the window.getSelection function there won't be any problem since that function is called only when a copy is performed, and we are 100% sure that when you copy a text, the userscript has already been loaded.

Conclusion

The best and cleanest solution is obviously the first one, since it disables a practically useless API.

The second one is valid too, but you will lose any of the ShareThis functionality.

The third one is the most "hacky" one, but as a last resort it could work.

Robertof

Posted 2014-01-19T02:10:24.293

Reputation: 616

Re: the first one, does this block sites where you can click a button to copy some text? E.g., https://emojipedia.org/snowman/ . Empirically, in Firefox, it does not seem to. Cool. Re: blocking ShareThis, my debugger is now showing the scripts loading from the subdomains "l.sharethis.com" and "ws.sharethis.com". So I think the only full cure is blocking the full ShareThis.com domain, which should be no great loss. Thanks @Robertof!

– Conrad Meyer – 2019-01-10T19:53:23.723

Caution: disabling clipboard events can cause some sites to malfunction. For example, it causes Discord to crash when pasting into search. – Nicholas – 2019-08-26T14:34:41.933

Thank you very much! This is excellent. It's a shame that this is so difficult and has no perfect solution, but your answer appears to be the best information that is currently available on the internet. Very pleased to award you the bounty. – Jess Riedel – 2014-01-29T18:49:35.840

Yeah, I did quite a bit of research since that problem was affecting me as well. Thank you for the bounty by the way - this will help my "newcomer" StackExchange experience! – Robertof – 2014-01-29T18:55:54.533

2

These odious actions can be blocked on Chrome with the "Kill Evil" extension:

https://chrome.google.com/webstore/detail/kill-evil/epieehnpcepgfiildhdklacomihpoldk

(EDIT) This extension appears to cause weird issues on facebook, be sure to whitelist it.

seagull

Posted 2014-01-19T02:10:24.293

Reputation: 4 278

Excellent!

I've found three extensions which purport to solve this problem:

  1. Tynt Blocker.
  2. RightToCopy.
  3. Kill Evil (thanks to you, seagull).

Kill Evil is the one that worked to fix the especially nasty clipboard manipulations at phys.org, – Dave Burton – 2017-03-07T23:01:02.800

1no worries @DaveBurton – please do note that this extension causes many sites to behave rather strangely, though, so only enable it when you need it. – seagull – 2017-03-08T12:58:30.060

2Kill Evil supports an exclusion list or "whitelist," which is a list of sites for which you do NOT want the extension enabled (i.e., a "permit evil" list). That wasn't what I wanted: I just want to enable Kill Evil ONLY on a particular site (phys.org). So I ended up with this tricky "negative lookahead" regular expression, to disable Kill Evil except for that one site: ^https?://(?!([a-zA-Z0-9-.]*phys.org)) – Dave Burton – 2017-04-25T21:08:54.810

Love a cheeky bit of regex. Thanks for posting it. – seagull – 2017-04-27T08:10:49.973

1

On Chromium I had success with Absolute Enable Right Click & Copy extension.

Having installed it, you can add you user list of sites where the extension is enabled in the preferences. Or, when you are on a particular page and see that it tampers with your clipboard, you can enable this extension on the fly via its toolbar button (this automatically adds the site to your list).

Ruslan

Posted 2014-01-19T02:10:24.293

Reputation: 1 168

1

Although it is a Firefox addon, NoScript allows you to block JavaScript from any website by setting up an untrusted list.

nonterrorist

Posted 2014-01-19T02:10:24.293

Reputation: 474

2Thanks for the answer, but this isn't what I need. (I want something that doesn't break JavaScript everywhere on the site.) I'll edit the question to be more specific. – Jess Riedel – 2014-01-22T00:41:03.740

1

If you are looking for a way to automatically modify the HTML before it is displayed, then Greasemonkey is the tool, and you would need to create a user-script that that does the modification.

From the article Beginner Guide for Greasemonkey Scripts in Google Chrome :

Chrome now natively supports user scripts. You don’t have to install an extra extension to use them; in fact, Chrome treats each user script as an individual addon so you can easily manage and remove them.

There is a lot of information about Greasemonkey to be found on the Internet, including many tutorials. Most of them would be for Firefox, where Greasemonkey originated, but they also apply today to Chrome.

As the site responsible for the "Read more" is sharethis.com. If you block it, this should stop. If you have an installed security suite, use it to block the site. Otherwise you can block it using your hosts file.

sharethis.com is the provider that supplies Tynt to the two websites you gave me. Tynt looks to be rather a technology than a website, so there may of course be other such providers, But one has to hope that they would be rather rare.

harrymc

Posted 2014-01-19T02:10:24.293

Reputation: 306 093

OK, thanks. In principle just about anything on a website can be fixed if one applies a greasemonkey script. (At the least, I presume any JavaScript problem can be fixed by more JavaScript.) In order to accept this answer for the bounty, I'd need a lot more info on how to implement it. – Jess Riedel – 2014-01-22T20:53:34.923

What kind of info? One can start with wikipedia that gives the most important links including the wiki and userscripts.org.

– harrymc – 2014-01-23T06:47:08.337

Say, an explanation of how the non-Tynt implementations work and what exactly would be done about them? So far nothing you've written is even specific to this copy-paste problem (rather than JavaScript in general). – Jess Riedel – 2014-01-23T15:57:53.277

I have never met Tynt (no wonder since I use NoScript). From the two example websites in the article you linked-to, one has disappeared and the other doesn't use Tynt any more. Do you have an example? – harrymc – 2014-01-23T18:54:52.700

I have made an edit to add two example websites that continue to have this behavior even when I use a Tynt-blocking extension. – Jess Riedel – 2014-01-25T18:34:25.103

By using NoScript and authorizing scripts site-by-site, I have found that the site responsible for the "Read more" is sharethis.com, which is not mentioned in any of your links. If you block it, this should stop. If you have an installed security suite, use it to block the site. Otherwise you can block it using your hosts file.

– harrymc – 2014-01-25T19:19:44.517

Are you saying that sharethis.com provides the same functionality as Tynt? I really don't want to have to block a new domain every times a website goes with a new provider. I want a final solution that prevents a website from screwing with my clipboard functionality. – Jess Riedel – 2014-01-25T21:45:30.850

All I know is that sharethis.com is the provider that supplies Tynt to the two websites you gave me. Tynt looks to be rather a technology than a website, so there may of course be other such providers. But I think/hope they should be rather rare, because after all Tynt is a stupid service and more annoying than useful. – harrymc – 2014-01-25T21:56:09.380

0

Alternatively, use the "Copy as Plain Text" extension for Chrome. You then have an additional option in the menu besides normal copying. It has the upside that it works in Chrome and doesn't break anything. https://chrome.google.com/webstore/detail/copy-as-plain-text-amazin/mkkcgjeddgdnikkeoinjgbocghokolck?utm_source=chrome-app-launcher-info-dialog

Julian

Posted 2014-01-19T02:10:24.293

Reputation: 1

From the name, you would think it breaks copying formatted text? That's sometimes useful to some people. – Conrad Meyer – 2019-01-10T19:55:42.790