Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use this in browser #45

Open
gautamkrishnar opened this issue Dec 29, 2019 · 13 comments
Open

how to use this in browser #45

gautamkrishnar opened this issue Dec 29, 2019 · 13 comments

Comments

@gautamkrishnar
Copy link

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.

@sonnyp
Copy link
Owner

sonnyp commented Dec 29, 2019

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?

@gautamkrishnar
Copy link
Author

gautamkrishnar commented Dec 29, 2019

@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.

@sonnyp
Copy link
Owner

sonnyp commented Dec 29, 2019

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
Copy link
Owner

sonnyp commented Dec 29, 2019

@gautamkrishnar
Copy link
Author

Okay Thanks will check that 👍

@sonnyp
Copy link
Owner

sonnyp commented Dec 29, 2019

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

@sonnyp sonnyp reopened this Dec 29, 2019
@gautamkrishnar
Copy link
Author

👍 Thats great.

@gautamkrishnar
Copy link
Author

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

@ChiaYen-Kan
Copy link

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

@pierrbt
Copy link

pierrbt commented Aug 8, 2023

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

@ChiaYen-Kan
Copy link

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 c960ebb 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

@pierrbt
Copy link

pierrbt commented Aug 9, 2023

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

@ChiaYen-Kan
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants