Skip to content

Commit feb7ead

Browse files
committed
Backport window.open
1 parent 226e5b9 commit feb7ead

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

include/pyjs/pre_js/init.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Module._is_initialized = false
44

55
Module['init_phase_1'] = async function(prefix, python_version, verbose) {
66

7-
if(verbose){console.log("in init phase 1");}
7+
if(verbose){console.log("in init phase 1");}
88
let version_str = `${python_version[0]}.${python_version[1]}`;
99

1010
// list of python objects we need to delete when cleaning up
@@ -32,7 +32,7 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
3232

3333

3434
Module.create_directories(side_path);
35-
35+
3636

3737
Module["_interpreter"] = new Module["_Interpreter"]()
3838
var default_scope = Module["main_scope"]()
@@ -134,7 +134,7 @@ Module['init_phase_1'] = async function(prefix, python_version, verbose) {
134134
return ret['ret']
135135
}
136136
};
137-
if(verbose){console.log("in init phase 2 done");}
137+
if(verbose){console.log("in init phase 2 done");}
138138
}
139139

140140
Module['init_phase_2'] = function(prefix, python_version, verbose) {
@@ -231,17 +231,43 @@ _mock_termios()
231231
del _mock_termios
232232
233233
def _mock_webbrowser():
234+
webbrowser_mock = types.ModuleType("webbrowser")
235+
236+
def get():
237+
webbrowser_mock
238+
234239
def open(url, new=0, autoraise=True):
235-
pass
240+
import pyjs
241+
is_main_thread = pyjs.js.Function("""return typeof WorkerGlobalScope === "undefined" || !(self instanceof WorkerGlobalScope);""")()
242+
if is_main_thread:
243+
pyjs.js.window.open(url)
244+
else:
245+
# we're in a web worker
246+
# This is sent to the main thread, which will do the window.open if implemented
247+
obj = pyjs.js.Function("url","n",
248+
"""
249+
return {'OPEN_TAB':{'url': url, 'new': n}}
250+
"""
251+
)(url, new)
252+
pyjs.js.postMessage(obj)
253+
236254
def open_new(url):
237255
return open(url, 1)
256+
238257
def open_new_tab(url):
239258
return open(url, 2)
240259
241-
webbrowser_mock = types.ModuleType("webbrowser")
260+
webbrowser_mock.name = pyjs.js.Function("""
261+
return /firefox/i.test(navigator.userAgent) ? "firefox"
262+
: /edg/i.test(navigator.userAgent) ? "edge"
263+
: /chrome|crios/i.test(navigator.userAgent) ? "chrome"
264+
: /safari/i.test(navigator.userAgent) ? "safari"
265+
: "Unknown";""")()
266+
webbrowser_mock.get = get
242267
webbrowser_mock.open = open
243268
webbrowser_mock.open_new = open_new
244269
webbrowser_mock.open_new_tab = open_new_tab
270+
webbrowser_mock.Error = RuntimeError
245271
246272
sys.modules["webbrowser"] = webbrowser_mock
247273
_mock_webbrowser()

tests/tests/test_pyjs.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
if sys.version_info[0] == 3 and sys.version_info[1] >= 13:
2424
def test_ssl_import():
2525
import ssl
26-
26+
2727
def test_import_lzma():
2828
import lzma
29-
29+
3030
def test_js_submodule():
3131
from pyjs.js import Function
3232

@@ -247,8 +247,8 @@ def test_to_js_dict():
247247

248248

249249
def test_bytes_to_js():
250-
pyjs.to_js(b"\x00").byteLength == 1
251-
250+
pyjs.to_js(b"\x00").byteLength == 1
251+
252252

253253

254254
def test_to_js_none():
@@ -393,7 +393,7 @@ def implicit_to_js(self):
393393
obj = pyjs.js_object()
394394
obj["the_value"] = self.value
395395
return obj
396-
396+
397397
class Bar(object):
398398
def __init__(self, value):
399399
self.value = value
@@ -403,7 +403,7 @@ def explicit_to_js(self):
403403
obj = pyjs.js_object()
404404
obj["the_value"] = self.value
405405
return obj
406-
406+
407407
class FooBarInstance(object):
408408
def __init__(self, value):
409409
self.value = value
@@ -419,8 +419,8 @@ def implicit_to_js(self):
419419
obj = pyjs.js_object()
420420
obj["the_implicit_value"] = self.value
421421
return obj
422-
423-
422+
423+
424424

425425
foo_instance = Foo(42)
426426
js_function = pyjs.js.Function("instance", """
@@ -439,7 +439,7 @@ def implicit_to_js(self):
439439
""")
440440
assert js_function(foo_bar_instance) is True
441441
assert js_function(pyjs.to_js(foo_bar_instance)) is False
442-
442+
443443
js_function = pyjs.js.Function("instance", """
444444
return instance.the_explicit_value === 42;
445445
""")
@@ -464,13 +464,13 @@ def test_literal_map():
464464
if (converted.get("foo") !== 1 || converted.get("bar") !== "bar" || !converted.get("foobar").includes("foo") || !converted.get("foobar").includes("bar")) {
465465
throw new Error("converted does not have the correct keys");
466466
}
467-
// ensure we can access the values via .
467+
// ensure we can access the values via .
468468
if (converted.foo !== 2 || converted.bar !== "bar" || !converted.foobar.includes("foo") || !converted.foobar.includes("bar")) {
469469
throw new Error("converted does not have the correct values when accessed via .");
470470
}
471471
return true;
472472
""")
473-
473+
474474

475475
def test_del_attr():
476476
obj = eval_jsfunc(
@@ -696,7 +696,3 @@ def test_imports_sys():
696696

697697
def test_webbrowser():
698698
from webbrowser import open, open_new, open_new_tab
699-
700-
open("google.com")
701-
open_new("google.com")
702-
open_new_tab("google.com")

0 commit comments

Comments
 (0)