Skip to content

Instantly share code, notes, and snippets.

@schetle
Last active May 11, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schetle/8ea7451215b89c4cc28506f96e9f384a to your computer and use it in GitHub Desktop.
Save schetle/8ea7451215b89c4cc28506f96e9f384a to your computer and use it in GitHub Desktop.
Basic Chrome Extension using V3, using Electron supported APIs
console.log("Hello, I'm a service worker");
chrome.runtime.onInstalled.addListener((evt) => {
console.log("Hello, I was just installed.");
});
chrome.runtime.onMessage.addListener((message, sender, reply) => {
console.log(`Hello, I'm a service worker and I just received a message.`);
reply("Hello sender, I received your message.");
});
console.log("I'm a content script, and I'm going to try to send a message to anyone that is listening.");
chrome.runtime.sendMessage("Hello from content script", function(response) {
console.log(`I have receipted a response: ${response}`);
});
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
'session': 'persist:1234'
}
})
// and load the index.html of the app.
mainWindow.loadURL('https://www.electronjs.org')
// Open the DevTools.
mainWindow.webContents.openDevTools()
mainWindow.webContents.session.loadExtension(path.join(__dirname)).then((id) => {
console.log(`Loaded extension ${id.name}`);
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "V3 service_worker test",
"version": "1.0.0",
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start"
}
],
"manifest_version": 3
}
{
"name": "sassy-tennis-travel-a881u",
"productName": "sassy-tennis-travel-a881u",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "schetle",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "18.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment