Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 702520489
  • Loading branch information
colaboratory-team committed Dec 14, 2024
1 parent cfe2b56 commit 68d05d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
24 changes: 24 additions & 0 deletions help.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<meta charset="utf-8">
<html lang="en">
<head>
<title>Open in Colab</title>
<style>
body {
width: 20rem;
}
</style>
</head>
<body>
<p>
This page is not supported.
</p>
<p>
<a href="https://chromewebstore.google.com/detail/open-in-colab/iogfkhleblhcpcekbiedikdehleodpjo"
target="_blank" aria-label="Open in Colab (opens in new tab)">
Open in Colab
</a>
supports GitHub-hosted .ipynb files.
</p>
</body>
</html>
18 changes: 16 additions & 2 deletions service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,35 @@

import {githubToColabUrl} from './parse';

// Details for calls to chrome.action.setPopup; see
// https://developer.chrome.com/docs/extensions/reference/api/action#method-setPopup.
const EMPTY_POPUP = {
popup: ''
};
const HELP_POPUP = {
popup: 'help.html'
};

// 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) => {
chrome.action.onClicked.addListener(async (tab: chrome.tabs.Tab) => {
if (!tab.url) {
console.warn('Open in Colab was invoked without a URL.');
return;
}
const colabUrl = githubToColabUrl(tab.url);
if (!colabUrl) {
// Set and show a helpful popup page when the extension icon is clicked on
// an invalid URL, then unset it in case the next click is valid.
await chrome.action.setPopup(HELP_POPUP);
await chrome.action.openPopup();
await chrome.action.setPopup(EMPTY_POPUP);
console.warn(`Current page (${
tab.url}) is not recognized as a GitHub-hosted notebook.`);
return;
}
chrome.tabs.create({'url': colabUrl});
await chrome.tabs.create({'url': colabUrl});
});

0 comments on commit 68d05d7

Please sign in to comment.