create a light gistlib.com extension for chrome in javascript

To create a Chrome extension for the GistLib website, you can use the following steps:

  1. Create a new folder for the extension, with any name you want.
  2. Create a new file named manifest.json in the root of the folder, and add the following code:
index.tsx
{
   "name": "GistLib Extension",
   "description": "This extension allows you to quickly save your code snippets to GistLib.",
   "version": "1.0",
   "manifest_version": 2,
   "permissions": ["tabs", "activeTab"],
   "browser_action": {
      "default_icon": {
         "16": "icon16.png",
         "32": "icon32.png"
      },
      "default_title": "Save to GistLib",
      "default_popup": "popup.html"
   }
}
414 chars
16 lines

This defines the basic information for the extension, including the permissions it requires, how the extension is displayed in the browser UI, and the location of the popup HTML file.

  1. Create two icon files in the root folder, named icon16.png and icon32.png. These will be used as the extension icons.
  2. Create a new file named popup.html in the root folder, and add the following code:
index.tsx
<!DOCTYPE html>
<html>
   <head>
      <title>Save to GistLib</title>
      <script src="popup.js"></script>
   </head>
   <body>
      <button id="saveToGistLibButton">Save to GistLib</button>
   </body>
</html>
213 chars
11 lines

This is the popup HTML file that will be displayed when the user clicks on the extension icon. It includes a button that will trigger the save to GistLib functionality.

  1. Create a new file named popup.js in the root folder, and add the following code:
index.tsx
document.addEventListener("DOMContentLoaded", function() {
   var saveToGistLibButton = document.getElementById("saveToGistLibButton");
   saveToGistLibButton.addEventListener("click", function() {
      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
         var activeTab = tabs[0];
         chrome.tabs.sendMessage(activeTab.id, {"message": "save_to_gistlib"});
      });
   });
});
411 chars
10 lines

This code listens for the button click, and sends a message to the active tab in the browser to save the code snippet to GistLib.

  1. Create a new file named content.js in the root folder, and add the following code:
index.tsx
chrome.runtime.onMessage.addListener(
   function(request, sender, sendResponse) {
      if( request.message === "save_to_gistlib" ) {
          // Code to save the current code snippet to GistLib
      }
   }
);
213 chars
8 lines

This code listens for the message sent by the popup script, and triggers the save to GistLib functionality.

  1. Load the extension by going to chrome://extensions/ in your Chrome browser, and clicking on "Load unpacked extension". Select the folder containing your extension files.
  2. Test the extension by navigating to a code snippet page, clicking on the extension icon, and clicking on the "Save to GistLib" button. The code snippet should be saved to GistLib.

gistlibby LogSnag