From 7f0f135376ae0da83d3511f1cd948416a4ab3e95 Mon Sep 17 00:00:00 2001 From: Google Colaboratory Team Date: Tue, 10 Dec 2024 10:14:41 -0800 Subject: [PATCH] No public description PiperOrigin-RevId: 704754659 --- service_worker.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 service_worker.ts diff --git a/service_worker.ts b/service_worker.ts new file mode 100644 index 0000000..4289388 --- /dev/null +++ b/service_worker.ts @@ -0,0 +1,50 @@ +/** + * @license + * Copyright 2018 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 + * + * https://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. + */ + +// 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.`); + } +});