Henry Sanger
Henry Sanger

Reputation: 143

Can't seed torrents with WebTorrent in Chrome extension service worker

I'm having some trouble using WebTorrent in a Chrome extension. I can download torrents with a service worker, but I can't seed them.

When I load the extension below, it prints a magnet link, as expected. However, when I try to download the file it's seeding with the webpage below, it doesn't work. The download never starts. Other WebTorrent clients like instant.io also don't work.

If I create a second webpage and copy the extension's code there, it works perfectly.

I am aware that service workers "go to sleep" after 30 seconds, or 5 minutes if there's an open port. I've checked, and the service worker is running.

Chrome Extension

background.js

importScripts('webtorrent.min.js');

var client = new WebTorrent();
client.seed(new File(['Testing testing'], 'test.txt'), function(torrent) {
    console.log(torrent.magnetURI);
});

manifest.json

{
    "manifest_version": 3,
    "name": "Testing",
    "version": "1.0",
    "background": {
        "service_worker": "background.js"
    }
}

Webpage

main.js

var client = new WebTorrent();
client.add('magnet:?xt=urn:btih:5825068c75bede26bb19f53...', function(torrent) {
    torrent.files[0].appendTo('body');
});

index.html

<!DOCTYPE html>
<html>
    <head>
        <script src="webtorrent.min.js"></script>
        <script src="main.js"></script>
    </head>
    <body></body>
</html>

I'm using the latest versions of the Brave browser and WebTorrent. Any help would be appreciated.

Upvotes: 0

Views: 329

Answers (0)

Related Questions