Skip to content

Instantly share code, notes, and snippets.

@Snarp
Last active May 10, 2024 19:41
Show Gist options
  • Save Snarp/6c06f60899ca3f43bdd3d8170885f83f to your computer and use it in GitHub Desktop.
Save Snarp/6c06f60899ca3f43bdd3d8170885f83f to your computer and use it in GitHub Desktop.
Script to allow copying from a protected Google Doc
/*
<https://stackoverflow.com/questions/40296831/is-it-possible-to-force-a-copy-of-a-protected-google-doc>
NOTE - 2021-05-24
-----------------
The script below isn't the fastest way to copy-and-paste from a protected
Google Doc. Before trying it, I'd suggest following MikoFrosty's advice from
the comments:
1) Change the end of the document url from `/edit` to `/mobilebasic`.
2) Disable Javascript (as described in steps 2 and 3 below).
3) Keeping the console open, reload the page.
That should be enough to allow you to copy freely. The script here is
*probably* only necessary if the /mobilebasic view isn't rendering something
correctly.
HOW TO USE
----------
1) Open the protected Doc using Chrome. Scroll all the way down to the bottom
to make sure that it is entirely loaded.
2) Open Chrome Developer Tools (<Ctrl+Shift+I>), and click "Console" to open
the Javascript Console.
3) Open the Command Console (<Ctrl+Shift+P>). Type "javascript", and select
the option "Disable Javascript" when it appears.
4) Copy the script below into the Javascript console.
5) Leaving the Javascript Console open, you may now copy-and-paste from the
document as normal. (Some formatting may be broken. Closing the console may
re-lock copy-and-pasting.)
6) (OPTIONAL) To copy the entire document as plain text (no formatting), type
the following into the console:
document.body.innerText
The text will be loaded into the Console itself; copy it by clicking the
gray-highlighted "COPY" at the end.
*/
function rtcScript() {
document.oncontextmenu = null;
document.onselectstart = null;
document.onmousedown = null;
document.onclick = null;
document.oncopy = null;
document.oncut = null;
var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
elements[i].oncontextmenu = null;
elements[i].onselectstart = null;
elements[i].onmousedown = null;
elements[i].oncopy = null;
elements[i].oncut = null;
}
function preventShareThis() {
document.getSelection = window.getSelection = function() {
return {isCollapsed: true};
}
}
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].src.indexOf('w.sharethis.com') > -1) {
preventShareThis();
}
}
if (typeof Tynt != 'undefined') {
Tynt = null;
}
}
rtcScript();
setInterval(rtcScript, 2000);
@fpenguin
Copy link

It didn't work for me, but I found a work around.

  1. Create a new blank google sheet. http://sheet.new is also a nifty trick.
  2. Enter the following formula on Cell A1
    =importrange("https://docs.google.com/spreadsheets/d/{ProtectedGoogleSheetID}/edit","{SheetName!Range}")
    make sure to replace the {ProtectedGoogleSheetID} as well as {SheetName!Range => Sheet1!A1:C999 for example}.

It's not editable yet, but you can copy the content.
(optional) Select All (Cmd+A), Copy content (Cmd+C), Paste values only (Cmd+Shift+V)

@bhung-chung
Copy link

Good Lord.
Great help here. Thanks, sir

@mitar
Copy link

mitar commented Sep 1, 2021

I would update the instructions to:

  1. Change the end of the document url from /edit to /mobilebasic.
  2. Disable Javascript (as described in steps 2 and 3 below).
  3. Keeping the console open, reload the page. Select all + copy.
  4. Create a new blank Google Doc. Paste.

That retained all formatting for me perfectly.

@markjohnsonii
Copy link

markjohnsonii commented Oct 6, 2021

changing the URL end to the mobilebasic just results in a message "Sorry, unable to open the file at this time."

The end of the doc is /edit#gid=0 and when I change this to /mobilebasic I get that message.

@jd412369
Copy link

It is also possible to just do
1 - 3 description above
after disablejavascript just Ctrl + P to save as PDF

@marklu-wustl
Copy link

@jd412369 Bravo! bro!

@onurogut
Copy link

@mitar worked like a charm. btw you can just copy you don't need any other google doc

@le-dawg
Copy link

le-dawg commented May 16, 2022

@jd412369 @deadcat6 @Snarp @Snarp

NOT WORKING FOR GOOGLE SLIDES. FYI. Disabling javascript bricks the UI, obviously. PDF printing also is caught by the deactivated javascript environment. I hacked around a little but could not find a solution. What needs to be stopped in the loaded javascript VM is the function that prevents all these thigns from happening. What is the official name for this function?

@onurogut
Copy link

@le-dawg mitar's solution is worked for me.

@VHAE04
Copy link

VHAE04 commented Jun 23, 2022

i make tool crawl data it but it working for text https://www.youtube.com/watch?v=fMW65aVWOmg

@DavidBuchanan314
Copy link

DavidBuchanan314 commented Jul 13, 2022

Does not seems to work on google sheets, currently

Edit: solution - change the end of the url to /preview

Edit2: For excel users, I imagine this trick would work nicely, in combination with /preview https://twitter.com/asmith83/status/1549373680496656385

@sunshineangel9xx
Copy link

i make tool crawl data it but it working for text https://www.youtube.com/watch?v=fMW65aVWOmg

Can you share, thanks a lot.

@VHAE04
Copy link

VHAE04 commented Nov 12, 2022

i make tool crawl data it but it working for text https://www.youtube.com/watch?v=fMW65aVWOmg

Can you share, thanks a lot.

yes

@shixiaoyu
Copy link

nice hack!

@Akshay6890
Copy link

Hi! i tried using the script but it wasn't working. the middle pages were all missing and blank. Is there any updated script i could use?

As MikoFrosty says, for this script to catch the whole document without any blanks, you need to manually scroll down very slowly to let everything load, which is pretty slow and annoying; so, as an alternative, you can change the end of the document URL from "/edit" to "/mobilebasic".
As far as I can tell, the mobilebasic interface always loads the whole document automatically. I think it may sometimes drop some complex formatting, but that's probably not going to transfer perfectly regardless.

i've done that too. scrolling until the last page. when i try to change the edit into mobilebasic i can see every page's text but can't highlight and copy any.

Another workaround that worked for me: (For Chrome) once you have to document in mobilebasic view, use F12 to open the inspector, then F1 to get to the Preferences tab, scroll to the bottom of the tab and select "Disable Javascript." Once that is done leave the inspector open and reload the page, then you should be able to highlight and copy the entire doc.

This worked like a charm! 😃

@MikoFrosty
Copy link

Any updates? None of the afforementioned steps work for now.

The mobilebasic method still works for me
(copied from @mitar)

  • Change the end of the document url from /edit to /mobilebasic.
  • Disable Javascript (as described in steps 2 and 3 below).
  • Keeping the console open, reload the page. Select all + copy.
  • Create a new blank Google Doc. Paste.

What part of this process is not working for you?

@thankventi
Copy link

hi, does anyone know how to copy it not on mobile view? the doc i want to copy a table that appears differently on mobile and desktop (i cannot figure it out on my own :P)

@dasunherath10
Copy link

does that still works on google spreadsheet , when i do this it gives a random number

@deadcoder0904
Copy link

@DamiJJJ
Copy link

DamiJJJ commented Jul 12, 2023

Works perfectly fine. Script not needed. Just change URL and disable JS ;)

@onurogut
Copy link

onurogut commented Aug 8, 2023

Simply use
image
Reading mode button on MacOS, works as well maybe anyone needs it.

@senhalil
Copy link

senhalil commented Aug 8, 2023

@onurogut I get a page that only shows the file name as a title and File/Edit/View/Tools/Help/... as subtitles 🤔

@VHAE04
Copy link

VHAE04 commented Aug 20, 2023

@onurogut
Copy link

@senhalil I tried with example that @deadcoder0904 shared and I'm attached a screenshot:

command+shift+r to switch reading mode:

image

As you can see, I can select all text/images by just simply command+a, copy and paste it into notes:
image

@falconwoods
Copy link

I would update the instructions to:

  1. Change the end of the document url from /edit to /mobilebasic.
  2. Disable Javascript (as described in steps 2 and 3 below).
  3. Keeping the console open, reload the page. Select all + copy.
  4. Create a new blank Google Doc. Paste.

That retained all formatting for me perfectly.

It works!!

@SMPSERVERS
Copy link

IT WORKED

@Mohammed-Nalwala
Copy link

  1. Change the end of the document url from /edit to /mobilebasic.

when I add "mobilebasic" end of url for google sheet it says
"Sorry, unable to open the file at present.Please check the address and try again."

@GBG7
Copy link

GBG7 commented Dec 2, 2023

  1. Change the end of the document url from /edit to /mobilebasic.

when I add "mobilebasic" end of url for google sheet it says "Sorry, unable to open the file at present.Please check the address and try again."

same

@sheenk09
Copy link

it works!

@010DevX101
Copy link

I found a workaround in /mobilebasic without having to disable JavaScript. You just have to rename the docsshared-no-select class to anything else and the script that prevents you from copying stops working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment