From 405ba7006bf322b8959c6a1357239428dfca6f97 Mon Sep 17 00:00:00 2001 From: OblivCode <60195126+OblivCode@users.noreply.github.com> Date: Thu, 12 Aug 2021 23:38:05 +0100 Subject: [PATCH] Add files via upload --- extension/background.js | 62 +++++++++++++++++++++++++++++++++++++++++ extension/manifest.json | 13 +++++++++ 2 files changed, 75 insertions(+) create mode 100644 extension/background.js create mode 100644 extension/manifest.json diff --git a/extension/background.js b/extension/background.js new file mode 100644 index 0000000..24aa52c --- /dev/null +++ b/extension/background.js @@ -0,0 +1,62 @@ +var connected = false +var ws +function start() { + if(connected) {return} + ws = new WebSocket("ws://localhost:800") + + connected = true + ws.onopen = function() { + console.log('connected'); + connected = true + Update() + } + ws.onmessage = function(message) { + console.log(message) + if(message == "resend") { + Update() + } + } + ws.onclose = function() { + connected = false + } +} + +chrome.windows.onRemoved.addListener(function(windowid) { + if(connected) { + ws.send("closed") + ws.close(1, "window closed") + } +}) + +chrome.tabs.onActivated.addListener(function(activeInfo) { + Update() +}) +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { + if(connected) { + let bundle = { + "title": tab.title, + "url": tab.url + } + let jsonBundle = JSON.stringify(bundle) + console.log(jsonBundle) + ws.send(jsonBundle) + } +}) + +function Update() { + chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) { + let tab = tabs[0] + if(connected) { + let bundle = { + "title": tab.title, + "url": tab.url + } + let jsonBundle = JSON.stringify(bundle) + console.log(jsonBundle) + ws.send(jsonBundle) + } + }); +} + + +setInterval(start, 1000) \ No newline at end of file diff --git a/extension/manifest.json b/extension/manifest.json new file mode 100644 index 0000000..ea895b1 --- /dev/null +++ b/extension/manifest.json @@ -0,0 +1,13 @@ +{ + "name": "OBC's RP Extension", + "description": "Browser extension for OBC's Discord Rich Presence", + "version": "1.0.0", + "manifest_version": 3, + "permissions": [ + "activeTab", + "tabs" + ], + "background": { + "service_worker": "background.js" + } +} \ No newline at end of file