Skip to content

Instantly share code, notes, and snippets.

@Kenya-West
Last active December 31, 2020 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kenya-West/3e0e764a38feeff3e1b31ba09495eb88 to your computer and use it in GitHub Desktop.
Save Kenya-West/3e0e764a38feeff3e1b31ba09495eb88 to your computer and use it in GitHub Desktop.
A JS file for Tampermonkey that redirects from Google Document to Microsoft Office when you browse files from external websites

Description

A js file for Tampermonkey that redirects you from external file previewing on Google Documents to previewing the file on Microsoft Office. Microsoft's solution is better, so this is why this script exists.

How to contribute

Clone this project on GitHub and start your changes. If you want to use the script from source code, then create new script in Tampermonkey extension and put source code in opened window.

How to use this script?

You need Tampermonkey extension installed on your browser to use this style.

You can install the script from Greasyfork.

Attention!

This script is designed for new browsers, such as new versions of Chromium-based browsers (Microsoft Edge, Opera, Google Chrome, etc.) and Firefox. The script will not run on the browsers released earlier than 2019. Thank you.

P. S. Author is not affiliated in any of mentioned organizations, events and links.

// ==UserScript==
// @name Google Docs redirection to Microsoft Office
// @namespace https://docs.google.com/
// @version 1.00
// @description A JS file for Tampermonkey that redirects from Google Document to Microsoft Office when you browse files from external websites.
// @author Kenya-West
// @match https://docs.google.com/viewerng/viewer*
// @grant none
// @run-at document-start
// ==/UserScript==
class GoogleDocsRedirection {
constructor(url) {
this.start(url);
}
start(htmlClass) {
const destination = "https://view.officeapps.live.com/op/view.aspx?src=";
const url = this.getURL();
if (url) {
window.location = `${destination}${url.searchParams.get("url")}`;
}
}
getURL() {
const host = "docs.google.com"
const path = "/viewerng/viewer"
const url = new URL(location.href);
if (url.hostname === host &&
url.pathname === path &&
url.searchParams.get("url")) {
return url;
}
return null;
}
}
setTimeout(() => new GoogleDocsRedirection("body"), 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment