From eb30a77ce31c1a78c13bdc803d5400fdc974e64d Mon Sep 17 00:00:00 2001 From: Google Colaboratory Team Date: Wed, 11 Dec 2024 16:04:15 -0800 Subject: [PATCH] No public description PiperOrigin-RevId: 705273000 --- parse.ts | 40 ++++++++++++++++++++++++++++++++++++++++ service_worker.ts | 37 ++++++++++++------------------------- 2 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 parse.ts diff --git a/parse.ts b/parse.ts new file mode 100644 index 0000000..9c6e665 --- /dev/null +++ b/parse.ts @@ -0,0 +1,40 @@ +/** + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const COLAB_BASE_URL = 'https://colab.research.google.com/'; +const GITHUB_REPO_RE = /^https?:\/\/github\.com\/(.+)\/(.*\.ipynb)$/; +const GITHUB_GIST_RE = + /^https?:\/\/gist\.github\.com\/(.+)\/([a-f0-9]+(?:\#file\-.*\-ipynb)?)$/; + +/** + * Attempt to convert a GitHub notebook URL to a Colab URL. + * + * @param githubUrl The GitHub URL to convert. + * + * @return For valid GitHub URLs, a link to open the notebook in Colab, + * otherwise null. + */ +export function githubToColabUrl(githubUrl: string): string|null { + const repoMatch = GITHUB_REPO_RE.exec(githubUrl); + if (repoMatch) { + return COLAB_BASE_URL + ['github', repoMatch[1], repoMatch[2]].join('/'); + } + const gistMatch = GITHUB_GIST_RE.exec(githubUrl); + if (gistMatch) { + return COLAB_BASE_URL + ['gist', gistMatch[1], gistMatch[2]].join('/'); + } + return null; +} diff --git a/service_worker.ts b/service_worker.ts index 461b4e9..888ce33 100644 --- a/service_worker.ts +++ b/service_worker.ts @@ -15,36 +15,23 @@ * limitations under the License. */ -// This listener is called when the user clicks the extension icon. - -// If the current URL matches a Jupyter notebook hosted on github.com -// or on gist.github.com, this function will open a new tab and load -// the notebook into Colab. +import {githubToColabUrl} from './parse'; +// This listener is called when the user clicks the extension icon. +// +// If the current URL matches a Jupyter notebook hosted on github.com or on +// gist.github.com, this function will open a new tab and load the notebook into +// Colab. chrome.action.onClicked.addListener((tab: chrome.tabs.Tab) => { - const colab_url = 'https://colab.research.google.com/'; - const github = /^https?:\/\/github\.com\/(.+)\/(.*\.ipynb)$/; - const gist = - /^https?:\/\/gist\.github\.com\/(.+)\/([a-f0-9]+(?:\#file\-.*\-ipynb)?)$/; - if (!tab.url) { console.warn('Open in Colab was invoked without a URL.'); return; } - - let url = null; - - const githubPath = github.exec(tab.url); - const gistPath = gist.exec(tab.url); - if (githubPath) { - url = colab_url + ['github', githubPath[1], githubPath[2]].join('/'); - } else if (gistPath) { - url = colab_url + ['gist', gistPath[1], gistPath[2]].join('/'); - } - - if (url) { - chrome.tabs.create({'url': url}); - } else { - console.warn(`Current page (${tab.url}) is not recognized as a GitHub-hosted notebook.`); + const colabUrl = githubToColabUrl(tab.url); + if (!colabUrl) { + console.warn(`Current page (${ + tab.url}) is not recognized as a GitHub-hosted notebook.`); + return; } + chrome.tabs.create({'url': colabUrl}); });