Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705273000
  • Loading branch information
colaboratory-team committed Dec 12, 2024
1 parent 7360403 commit eb30a77
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
40 changes: 40 additions & 0 deletions parse.ts
Original file line number Diff line number Diff line change
@@ -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;
}
37 changes: 12 additions & 25 deletions service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});

0 comments on commit eb30a77

Please sign in to comment.