How do I use Discord-RPC with Chrome Extension?

3.8k Views Asked by At

I want to make a chrome extension which parses youtube/soundcloud/... pages and gets current song's name. Then it should update user's rich presence status in discord. Like a did it in python there.

I have that one so far. Title already parsed, there's no problem. I have a problem with that code works. Because it doesn't work.

const clientId = '605777168739598338';
const scopes = ['rpc', 'rpc.api'];

const client = new RPC.Client({ transport: 'websocket' });

client.connect();

function updatePresence(title, time, icon) {
    title = title.replace(/["]/g, "\\\"");
    client.setActivity({
        details: title,
        startTimestamp: time,
        largeImageKey: icon
    }, 9999)
}

I also tried raw websocket connection but I'm stupid..

UPD: The code above is in content.js.

browser.js is a file copied from root of module discord-rpc which i downloaded via npm.

manifest.json

{
  "manifest_version": 2,
  "name": "Tomori Player",
  "version": "0.1.0",
  "browser_action": {
     "default_icon": "icon.png"
    },
    "background": {
        "scripts": ["browser.js"]
    },
    "permissions": [
        "ws://localhost:6463/*",
        "tabs",
        "webRequest",
        "webRequestBlocking"
    ],
  "content_scripts": [
      {
        "matches": [
          "https://www.youtube.com/watch*",
          "https://youtube.com/watch*"
        ],
        "js": ["content.js"]
      }
    ]
}

P.S. I'm so sorry. I'm new in JS.

1

There are 1 best solutions below

0
On BEST ANSWER

There's a solution I found:

  • Chrome Extension parses page and sends sockets to another app
  • Another app on your PC gets these sockets and then sends RPC to Discord

Timeraa#7947 (PreMiD dev): Discord will disconnect almost immediately if you connect with a browser, trust me i tried. You will need to have an application running in the background

So, you can use PreMiD and push up PreMiD's presences list or make your own app to do it.