npm install ws
Last active
June 1, 2021 20:22
-
-
Save vasilvestre/ef5466a6b1d5a4e344ad6f1ef8c87199 to your computer and use it in GitHub Desktop.
Chrome extension ws connect + read current tab + node js ws server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ws = new WebSocket('ws://localhost:8088/post'); | |
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { | |
if (changeInfo.status === "complete") { | |
ws.send(tab.url); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Getting Started Example", | |
"description": "Build an Extension!", | |
"version": "1.0", | |
"manifest_version": 3, | |
"background": { | |
"service_worker": "background.js" | |
}, | |
"permissions": ["tabs","activeTab"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WebSocket = require('ws'); | |
const url = require('url'); | |
const wss = new WebSocket.Server({ port: 8088 }); | |
wss.on('connection', function connection(ws, request) { | |
const pathname = url.parse(request.url).pathname; | |
var msg = ''; | |
if (pathname === '/post') { | |
ws.on('message', function incoming(message) { | |
console.log('received: %s', message); | |
msg = message; | |
}); | |
} | |
if (pathname === '/get') { | |
ws.on('message', function incoming(message) { | |
console.log('sending : %s', message); | |
ws.send(msg); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment