1

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?

Community
  • 1
  • 1
Mike Ellis
  • 1,120
  • 1
  • 12
  • 27

2 Answers2

1

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

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
  • 1
    Thanks, Noam and I'll upvote for the quick response. That's one way to do it, for sure but the dependency on the UI makes me reluctant to invest effort into something Google may break tomorrow. I'm hoping someone can point me to API calls that may be a little more stable. – Mike Ellis Dec 14 '16 at 15:35
  • Thanks for the feedback. Using the Google Drive API would be much more reliable, and I would recommend that kind of solution instead. I'll do some research into that as well and get back to you – Noam Hacker Dec 14 '16 at 15:39
1

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.
Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
  • Just FYI: In both your answers, you should first check if the URL is to a domain/page which might/does contain a Google Doc. Right now, you are just blindly assuming that current page is to a Google Doc. While it is unlikely that either method will falsely identify some random page as an editable Google Doc, one never knows. – Makyen Dec 14 '16 at 16:11
  • 1
    This seems like a good approach. I also found a useful discussion of how to extract the fileid from a url at http://stackoverflow.com/questions/16840038/easiest-way-to-get-file-id-from-url-on-google-apps-script – Mike Ellis Dec 14 '16 at 17:05