Determine if active tab contains an editable Google Doc

100 views Asked by At

Suppose my extension has obtained the URL for the current active tab using the method suggested in How can I get the current tab URL for chrome extension?, e.g.

chrome.tabs.query({
    active: true,
    lastFocusedWindow: true
}, function (tabs) {
    // use first tab to obtain url
    var tab = tabs[0];
    var url = tab.url
});

How can I determine if the URL refers to a Google Doc to which I have editing rights? The distinction between ownership and being an invited collaborator is not important for this application. I'm interested only in Docs as opposed to Spreadsheets or Forms.

For context on what I'm trying to develop, see: How to manually generate notifications to a Google Doc collaborator?

2

There are 2 answers

2
Noam Hacker On BEST ANSWER

Alternate answer, based on the Google Drive API rather than the Google Docs UI.

Before doing any of this, make sure to declare permissions in the manifest (to only match URLs that look like docs.google.com/document/*).

Here is a broad overview of the steps you could follow:

  1. GET a file, which will return some metadata. You can use the URL you have to extract the relevant ID which is used in the GET request.

  2. Use this metadata file resource to retrieve a permissions resource

  3. In the permissions resource, look at the role attribute: it will be either owner, reader, or writer. You will not have editing rights if you are a reader, but should have editing rights otherwise.
2
Noam Hacker On

Here is a side by side view of a google doc, where I created a doc, generated a sharing link, and opened it in a browser where I was not signed in to google. I would suggest using a content script to insert a "find" function which would return either true or false if it can locate the "view only" button in the DOM ("view only" meaning you do not have edit permissions). You could make the content script match URLs that look like docs.google.com/document/* only.

Caution: google changes UI pretty frequently so this may not be future-proof. Try inspecting the source of google docs in both situations to look for more clues.

Side by side view:

side by side

Source code in the chrome devtools:

view only button source code