aria2.js icon indicating copy to clipboard operation
aria2.js copied to clipboard

how to use this in browser

Open gautamkrishnar opened this issue 5 years ago • 13 comments

I cant see any specific builds for browsers. I tried using browserifly but it didn't worked. Can you guys let me know how can we use this for web browsers.

gautamkrishnar avatar Dec 29 '19 07:12 gautamkrishnar

I tried using browserifly but it didn't worked

Browserify works fine with aria2.js, double check your configuration / usage .

We use to have the browser bundle shipped with aria2.js . I would be open to re-introduce it. I just assumed everybody used templates and build systems nowadays when writing web apps. What is your use case?

sonnyp avatar Dec 29 '19 12:12 sonnyp

@sonnyp thanks for the reply, I am developing a download manager extension for chrome. I tried making a file using the following command:

browserify index.js -o op.js

But when i tried to include it via script <script src="op.js" type="text/javascript"> i couldn't find the Aria2 object in the window. So that i can't do

var x = new Aria2();

Yes, Its better to have a browser bundle shipped with all releases.

gautamkrishnar avatar Dec 29 '19 12:12 gautamkrishnar

If you want to use a global you need to use the standalone mode.

Something like

browserify index.js -o Aria2.js -s Aria2

sonnyp avatar Dec 29 '19 12:12 sonnyp

See https://github.com/browserify/browserify#usage

sonnyp avatar Dec 29 '19 12:12 sonnyp

Okay Thanks will check that 👍

gautamkrishnar avatar Dec 29 '19 13:12 gautamkrishnar

I'll leave this open as a reminder to re-introduce the browser bundle

sonnyp avatar Dec 29 '19 13:12 sonnyp

👍 Thats great.

gautamkrishnar avatar Dec 29 '19 13:12 gautamkrishnar

@sonnyp any idea why you guys removed the browser bundle?

gautamkrishnar avatar May 24 '20 07:05 gautamkrishnar

Hi @sonnyp i try build from current source code and use "bundle.js" in browser, but i got a error

bundle.js:519 Uncaught ReferenceError: global is not defined
    at new JSONRPCClient (bundle.js:519:22)
    at new Aria2 (bundle.js:706:3)
    at aria2.js.html:6:25

bundle

this is my test code

<html>
    <head>
        <meta charset="utf-8">
        <script type="application/javascript" src="bundle.js"></script>
        <script>
            var aria2 = new Aria2();
            aria2.call("addUri",
                ["https://fastly.picsum.photos/id/337/536/354.jpg?hmac=T7nJ4MjfcTPFuaaLRH_1zwqyOFzmldcV-f5abNr2MyE"],
            );
        </script>
    </head>
</html>

how to fix this error?

thank you

ChiaYen-Kan avatar Mar 23 '23 06:03 ChiaYen-Kan

@ChiaYen-Kan, do you still want to make this work ? I've found a solution

pierrbt avatar Aug 08 '23 21:08 pierrbt

Hi @pierrbt i also have a solution, it's work, but i don't know is good solution or bad solution 🤔 following is my solution, base on https://github.com/sonnyp/aria2.js/commit/c960ebb1395cb08c51ec5a796b03f5609a92f459 in main branch

From 26b51a31699d3fd299dafe6566f3c79480301286 Mon Sep 17 00:00:00 2001
From: ChiaYen Kan <***>
Date: Fri, 24 Mar 2023 16:37:03 +0800
Subject: [PATCH] fix can't run in browser

---
 src/JSONRPCClient.js | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/JSONRPCClient.js b/src/JSONRPCClient.js
index 8bab3aa..059babc 100644
--- a/src/JSONRPCClient.js
+++ b/src/JSONRPCClient.js
@@ -4,6 +4,8 @@ import Deferred from "./Deferred.js";
 import promiseEvent from "./promiseEvent.js";
 import JSONRPCError from "./JSONRPCError.js";
 
+var global = global || globalThis;
+
 class JSONRPCClient extends EventEmitter {
   constructor(options) {
     super();
@@ -12,7 +14,7 @@ class JSONRPCClient extends EventEmitter {
 
     Object.assign(
       this,
-      { WebSocket: global.WebSocket, fetch: global.fetch.bind(this) },
+      { WebSocket: global.WebSocket, fetch: global.fetch.bind(global) },
       this.constructor.defaultOptions,
       options
     );
-- 
2.41.0.windows.3

thank you

ChiaYen-Kan avatar Aug 09 '23 00:08 ChiaYen-Kan

@ChiaYen-Kan, Personally, I've just changed global to window, and added the events depencies.

pierrbt avatar Aug 09 '23 00:08 pierrbt

Hi @pierrbt i am not a web developer, i just want to it can work in my little project may be someone can make it best thank you

ChiaYen-Kan avatar Aug 09 '23 01:08 ChiaYen-Kan