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

Loading blink.js on loadURL #320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions res/blink.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// Comms stuff

var ws = location.href.replace("http", "ws");

// use specified url if it exists (supplied by createWindow opts)
if (document.ws) {
ws = document.ws;
}

if (!/\/\d+$/.test(ws)) {
ws += '/' + id;
}
Expand Down
34 changes: 33 additions & 1 deletion src/AtomShell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,43 @@ app.on("ready", function() {
// Window creation
var windows = {};

function createWindow(opts) {
// opts are Electron BrowserWindow options and comUrl is the url to the local
// server that will be used to communicate with the window.
function createWindow(opts, comUrl) {
var win = new BrowserWindow(opts);
windows[win.id] = win;
if (opts.url) {
win.loadURL(opts.url);

windows[win.id].comUrl = comUrl;

// load blink.js and webio.bundle.js whenever navigation is performed
win.webContents.on('did-finish-load', function() {
// load files from the local server instead of provided url for content
win.webContents.executeJavaScript(`
// is blink.js already loaded?
if (typeof Blink != 'object') {
var comUrl = '${comUrl ? comUrl : windows[win.id].comUrl}';
var port = comUrl.split(":").pop().split("/")[0];
var id = comUrl.split("/").pop()[0];
var urlParams = new URLSearchParams(comUrl);
var callback_id = urlParams.get('callback');

document.ws = comUrl.replace("http", "ws");

var script = document.createElement('script');
script.defer = true;
script.type = 'text/javascript';
script.src = 'http://localhost:' + port + '/blink.js';
document.head.appendChild(script);

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://localhost:' + port + '/webio.bundle.js';
document.head.appendChild(script);
}
`);
});
}
win.setMenu(null);

Expand Down
43 changes: 32 additions & 11 deletions src/AtomShell/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ const window_defaults = Dict(
:icon => resolve_blink_asset("deps", "julia.png")
)

raw_window(a::Electron, opts) = @js a createWindow($(merge(window_defaults, opts)))
raw_window(a::Electron, opts, comurl) = @js a createWindow($(merge(window_defaults, opts)), $(comurl))

function Window(a::Shell, opts::AbstractDict = Dict(); async=false, body=nothing)
# TODO: Custom urls don't support async b/c don't load Blink.js. (Same as https://github.com/JunoLab/Blink.jl/issues/150)
window = haskey(opts, :url) ?
Window(raw_window(a, opts), a, nothing, nothing) :
initWindowUrl(a, Page(), opts) :
Window(a, Page(), opts; async=async)
isnothing(body) || body!(window, body)
return window
Expand All @@ -57,8 +57,37 @@ function Window(a::Shell, content::Page, opts::AbstractDict = Dict(); async=fals

# Create the window.
opts = merge(opts, Dict(:url => url))
w = Window(raw_window(a, opts), a, content, nothing)
w = Window(raw_window(a, opts, url), a, content, nothing)

initializeWindowAsync!(w, callback_cond)

if !async
wait(w)
end

isnothing(body) || body!(w, body)

return w
end

"""
initWindowUrl(a::Shell, content::Page, opts::AbstractDict = Dict())

Builds window that loads content from specified url in opts.
"""
function initWindowUrl(a::Shell, content::Page, opts::AbstractDict = Dict())
id, callback_cond = Blink.callback!()
comurl = Blink.localurl(content) * "?callback=$id"

# supply url of server (comurl) since :url in opts is url to load content from
w = Window(raw_window(a, opts, comurl), a, content, nothing)

initializeWindowAsync!(w, callback_cond)

return w
end

function initializeWindowAsync!(w::Window, callback_cond::Threads.Condition)
# Note: we have to use a task here because of the use of Condition throughout
# the codebase (it might be better to use Channel or Future which are not
# edge-triggered). We also need to initialize this after the Window
Expand All @@ -71,14 +100,6 @@ function Window(a::Shell, content::Page, opts::AbstractDict = Dict(); async=fals
exception=(exc, catch_backtrace()),
)
end

if !async
wait(w)
end

isnothing(body) || body!(w, body)

return w
end

function initwindow!(w::Window, callback_cond::Threads.Condition)
Expand Down
5 changes: 4 additions & 1 deletion src/content/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ function ws_handler(ws)
p.sock = ws
@async @errs get(handlers(p), "init", identity)(p)
try
put!(p.cb, true)
# set only on first websocket connection initialization
if !isready(p.cb)
put!(p.cb, true)
end
catch e
@warn e
end
Expand Down
11 changes: 11 additions & 0 deletions test/content/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ w = Window(Blink.Dict(:show => false), async=false);
@test (@js w document.getElementById("b").innerHTML) == "bye"
end

temp_dir = tempdir()
temp_html = joinpath(temp_dir, "temp.html")
write(temp_html, """<html><head><title>Test</title></head><body><div id="a">Test</div></body></html>""")
w_url = Window(Blink.Dict(:url => "file://" * temp_html, :show => false), async=false);
@testset "blink.js is included in document when using loadurl" begin
sleep(1) # wait for page to load

@test (@js w_url document.getElementById("a").innerHTML) == "Test"
@test (@js w_url [].filter.call(document.scripts, function (script) return script.src.includes("blink.js") end).length) == 1
end


# Test `fade` parameter and scripts:
# Must create a new window to ensure javascript is reset.
Expand Down