Skip to content

Commit 8c345c3

Browse files
authored
Enhancement: Change installation process (#275)
* Putting component in the config * Removed checking browser features * Added error log * Moved fetching session url to extension * Updated metatag message * Updated igniter * Updated README * Added condition for highlighting in scripts * Browser features enabled in dev * Moved border * Updated colors of disconnect page in extension * Updated README * Updated static files * Set server to `true` * Better handling of highlighting feature * Moved warning in README to the bottom * Reverted README updates
1 parent be1754f commit 8c345c3

14 files changed

Lines changed: 214 additions & 92 deletions

File tree

assets/js/client.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,42 @@ function getSessionId() {
1818
}
1919
}
2020

21+
function handleMetaTagError() {
22+
const message = `
23+
LiveDebugger meta tag not found!
24+
If you have recently bumped LiveDebugger version, please update your layout according to the instructions in the GitHub README.
25+
You can find it here: https://github.com/software-mansion/live-debugger#installation
26+
`;
27+
28+
throw new Error(message);
29+
}
30+
31+
function debugButtonEnabled() {
32+
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
33+
34+
if (metaTag) {
35+
return metaTag.hasAttribute('debug-button');
36+
} else {
37+
handleMetaTagError();
38+
}
39+
}
40+
41+
function highlightingEnabled() {
42+
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
43+
44+
if (metaTag) {
45+
return metaTag.hasAttribute('highlighting');
46+
}
47+
}
48+
2149
function getLiveDebuggerBaseURL() {
22-
return document
23-
.getElementById('live-debugger-scripts')
24-
.src.replace('/assets/live_debugger/client.js', '');
50+
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
51+
52+
if (metaTag) {
53+
return metaTag.getAttribute('url');
54+
} else {
55+
handleMetaTagError();
56+
}
2557
}
2658

2759
function getSessionURL(baseURL) {
@@ -31,20 +63,17 @@ function getSessionURL(baseURL) {
3163
return `${baseURL}/${session_path}`;
3264
}
3365

34-
window.getLiveDebuggerURL = function () {
35-
const baseURL = getLiveDebuggerBaseURL();
36-
const sessionURL = getSessionURL(baseURL);
37-
38-
return sessionURL;
39-
};
40-
4166
window.document.addEventListener('DOMContentLoaded', function () {
4267
const baseURL = getLiveDebuggerBaseURL();
4368
const sessionURL = getSessionURL(baseURL);
4469

45-
initDebugButton(sessionURL);
70+
if (debugButtonEnabled()) {
71+
initDebugButton(sessionURL);
72+
}
4673

47-
initHighlight();
74+
if (highlightingEnabled()) {
75+
initHighlight();
76+
}
4877

4978
// Finalize
5079
console.info(`LiveDebugger available at: ${baseURL}`);

config/config.exs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ if config_env() == :dev do
4747
]
4848
]
4949

50-
config :live_debugger, server: true
51-
52-
config :live_debugger, browser_features?: true
5350
config :live_debugger, experimental_features: :all
5451
end
5552

dev/layout.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ defmodule LiveDebuggerDev.Layout do
2323
</script>
2424
<script src="/assets/phoenix_live_view/phoenix_live_view.js">
2525
</script>
26-
<%= if Application.get_env(:live_debugger, :browser_features?) do %>
27-
<script id="live-debugger-scripts" src={Application.get_env(:live_debugger, :assets_url)}>
28-
</script>
29-
<% end %>
26+
<%= Application.get_env(:live_debugger, :live_debugger_tags) %>
3027
<script>
3128
// Set global hooks and uploaders objects to be used by the LiveSocket,
3229
// so they can be overwritten in user provided templates.

devtools/devtools.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
11
function getLiveDebuggerSessionURL() {
22
return new Promise((resolve, reject) => {
3-
chrome.devtools.inspectedWindow.eval(
4-
"getLiveDebuggerURL()",
5-
(result, isException) => {
6-
if (isException) {
7-
reject(isException);
8-
} else {
9-
resolve(result);
3+
const script = `
4+
(function() {
5+
function getSessionId() {
6+
let el;
7+
if ((el = document.querySelector('[data-phx-main]'))) {
8+
return el.id;
9+
}
10+
if ((el = document.querySelector('[id^="phx-"]'))) {
11+
return el.id;
12+
}
13+
if ((el = document.querySelector('[data-phx-root-id]'))) {
14+
return el.getAttribute('data-phx-root-id');
15+
}
16+
return null;
1017
}
18+
19+
function handleMetaTagError() {
20+
throw new Error("LiveDebugger meta tag not found!");
21+
}
22+
23+
function getLiveDebuggerBaseURL() {
24+
const metaTag = document.querySelector('meta[name="live-debugger-config"]');
25+
if (metaTag) {
26+
return metaTag.getAttribute('url');
27+
} else {
28+
handleMetaTagError();
29+
}
30+
}
31+
32+
function getSessionURL(baseURL) {
33+
const session_id = getSessionId();
34+
const session_path = session_id ? \`transport_pid/\${session_id}\` : '';
35+
return \`\${baseURL}/\${session_path}\`;
36+
}
37+
38+
const baseURL = getLiveDebuggerBaseURL();
39+
return getSessionURL(baseURL);
40+
})();
41+
`;
42+
43+
chrome.devtools.inspectedWindow.eval(script, (result, isException) => {
44+
if (isException || !result) {
45+
reject(new Error("Error fetching LiveDebugger session URL"));
46+
} else {
47+
resolve(result);
1148
}
12-
);
49+
});
1350
});
1451
}
1552

devtools/panel.html

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<style>
44
body {
55
margin: 0;
6-
background-color: rgb(241 245 249);
6+
background-color: #f1f5f9;
77
}
88
iframe {
99
border: none;
@@ -21,7 +21,7 @@
2121
#error-info svg {
2222
width: 48px;
2323
height: 48px;
24-
color: rgb(76 43 138);
24+
color: #ef4444;
2525
}
2626
#error-info div {
2727
font-weight: 600;
@@ -33,6 +33,14 @@
3333
max-width: 70%;
3434
margin: 0;
3535
}
36+
a {
37+
color: #001a72;
38+
text-decoration: none;
39+
}
40+
a:hover {
41+
color: #33498b;
42+
text-decoration: none;
43+
}
3644
</style>
3745
</head>
3846
<body>
@@ -59,8 +67,15 @@
5967
</svg>
6068
<div>Debugger disconnected</div>
6169
<p>
62-
Couldn't find url. Ensure you've turned on browser features in the
63-
config and added LiveDebugger scripts to your application root layout.
70+
Couldn't find LiveDebugger url - please update your root layout
71+
according to the instructions in the GitHub README.
72+
<br />
73+
You can find it
74+
<a
75+
target="_blank"
76+
href="https://github.com/software-mansion/live-debugger#installation"
77+
>here</a
78+
>
6479
</p>
6580
</div>
6681

lib/live_debugger.ex

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,8 @@ defmodule LiveDebugger do
1616
def start(_type, _args) do
1717
config = Application.get_all_env(@app_name)
1818

19-
default_adapter = default_adapter()
20-
ip = Keyword.get(config, :ip, @default_ip)
21-
ip_string = ip |> :inet.ntoa() |> List.to_string()
22-
port = Keyword.get(config, :port, @default_port)
23-
24-
endpoint_config =
25-
[
26-
http: [ip: ip, port: port],
27-
secret_key_base: Keyword.get(config, :secret_key_base, @default_secret_key_base),
28-
live_view: [signing_salt: Keyword.get(config, :signing_salt, @default_signing_salt)],
29-
adapter: Keyword.get(config, :adapter, default_adapter),
30-
live_reload: Keyword.get(config, :live_reload, []),
31-
server: Keyword.get(config, :server, false)
32-
]
33-
34-
Application.put_env(@app_name, LiveDebugger.Endpoint, endpoint_config)
35-
Application.put_env(@app_name, :assets_url, "http://#{ip_string}:#{port}/#{@assets_path}")
19+
put_endpoint_config(config)
20+
put_live_debugger_tags(config)
3621

3722
children = [
3823
{Phoenix.PubSub, name: LiveDebugger.PubSub},
@@ -62,4 +47,44 @@ defmodule LiveDebugger do
6247
{:error, _} -> Phoenix.Endpoint.Cowboy2Adapter
6348
end
6449
end
50+
51+
defp put_endpoint_config(config) do
52+
endpoint_config =
53+
[
54+
http: [
55+
ip: Keyword.get(config, :ip, @default_ip),
56+
port: Keyword.get(config, :port, @default_port)
57+
],
58+
secret_key_base: Keyword.get(config, :secret_key_base, @default_secret_key_base),
59+
live_view: [signing_salt: Keyword.get(config, :signing_salt, @default_signing_salt)],
60+
adapter: Keyword.get(config, :adapter, default_adapter()),
61+
live_reload: Keyword.get(config, :live_reload, []),
62+
server: true
63+
]
64+
65+
Application.put_env(@app_name, LiveDebugger.Endpoint, endpoint_config)
66+
end
67+
68+
defp put_live_debugger_tags(config) do
69+
ip_string = config |> Keyword.get(:ip, @default_ip) |> :inet.ntoa() |> List.to_string()
70+
port = Keyword.get(config, :port, @default_port)
71+
72+
browser_features? = Keyword.get(config, :browser_features?, true)
73+
debug_button? = Keyword.get(config, :debug_button?, true)
74+
highlighting? = Keyword.get(config, :highlighting?, true)
75+
76+
live_debugger_url = "http://#{ip_string}:#{port}"
77+
live_debugger_assets_url = "http://#{ip_string}:#{port}/#{@assets_path}"
78+
79+
assigns = %{
80+
url: live_debugger_url,
81+
assets_url: live_debugger_assets_url,
82+
browser_features?: browser_features?,
83+
debug_button?: debug_button?,
84+
highlighting?: highlighting?
85+
}
86+
87+
tags = LiveDebugger.Components.Config.live_debugger_tags(assigns)
88+
Application.put_env(@app_name, :live_debugger_tags, tags)
89+
end
6590
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule LiveDebugger.Components.Config do
2+
@moduledoc """
3+
Renders the LiveDebugger config meta tag and the browser features script.
4+
It is meant to be injected to the debugged application layout.
5+
"""
6+
7+
use Phoenix.Component
8+
9+
attr(:url, :string, required: true)
10+
attr(:assets_url, :string, required: true)
11+
attr(:browser_features?, :boolean, default: true)
12+
attr(:debug_button?, :boolean, default: true)
13+
attr(:highlighting?, :boolean, default: true)
14+
15+
def live_debugger_tags(assigns) do
16+
~H"""
17+
<meta
18+
name="live-debugger-config"
19+
url={@url}
20+
debug-button={@debug_button?}
21+
highlighting={@highlighting?}
22+
/>
23+
<%= if @browser_features? do %>
24+
<script src={@assets_url}>
25+
</script>
26+
<% end %>
27+
"""
28+
end
29+
end

lib/live_debugger/components/tree.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule LiveDebugger.Components.Tree do
3333
<div class={["w-full overflow-y-auto flex flex-col", @class]}>
3434
<div class="flex items-center justify-between">
3535
<div class="shrink-0 font-medium text-secondary-text px-6 py-3"><%= @title %></div>
36-
<%= if Application.get_env(:live_debugger, :browser_features?) && LiveDebugger.Feature.enabled?(:highlighting) do %>
36+
<%= if LiveDebugger.Feature.enabled?(:highlighting) do %>
3737
<.toggle_switch label="Highlight" checked={@highlight?} phx-click="toggle-highlight" />
3838
<% end %>
3939
</div>

lib/live_debugger/feature.ex

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
defmodule LiveDebugger.Feature do
22
@moduledoc """
3-
Feature flags for LiveDebugger. If you create a new feature, you need to add it to the @experimental_features list.
4-
This way we can easily find all the features that are experimental and not ready for production.
3+
Feature flags for LiveDebugger.
4+
If you create a new feature, create a new function here with defined rules for enabling it.
55
"""
66

7-
@experimental_features [
8-
:dark_mode,
9-
:highlighting,
10-
:callback_filters
11-
]
7+
def enabled?(:highlighting) do
8+
experimental_feature_enabled?(:highlighting) and
9+
Application.get_env(:live_debugger, :browser_features?, true) and
10+
Application.get_env(:live_debugger, :highlighting?, true)
11+
end
12+
13+
def enabled?(:dark_mode) do
14+
experimental_feature_enabled?(:dark_mode)
15+
end
1216

13-
def enabled?(feature_name) when feature_name in @experimental_features do
17+
def enabled?(:callback_filters) do
18+
experimental_feature_enabled?(:callback_filters)
19+
end
20+
21+
def enabled?(feature_name) do
22+
raise "Feature #{feature_name} is not allowed"
23+
end
24+
25+
defp experimental_feature_enabled?(feature_name) do
1426
case Application.get_env(:live_debugger, :experimental_features, false) do
1527
:all -> true
1628
features when is_list(features) -> Enum.member?(features, feature_name)
1729
_ -> false
1830
end
1931
end
20-
21-
def enabled?(feature_name) do
22-
raise "Feature #{feature_name} is not allowed"
23-
end
2432
end

lib/live_debugger/live_views/sidebar_live.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ defmodule LiveDebugger.LiveViews.SidebarLive do
8282
@impl true
8383
def render(assigns) do
8484
~H"""
85-
<div class="w-max flex bg-sidebar-bg shadow-custom border-x border-default-border h-full">
86-
<div class="hidden lg:flex max-h-full flex-col w-72 lg:w-80 gap-1 justify-between">
85+
<div class="w-max flex bg-sidebar-bg shadow-custom h-full">
86+
<div class="hidden lg:flex max-h-full flex-col w-72 border-x border-default-border lg:w-80 gap-1 justify-between">
8787
<.sidebar_content
8888
id="sidebar-content"
8989
lv_process={@lv_process}
@@ -157,8 +157,7 @@ defmodule LiveDebugger.LiveViews.SidebarLive do
157157
def handle_event("select_node", params, socket) do
158158
%{"node_id" => node_id, "search-attribute" => attr, "search-value" => val} = params
159159

160-
if Application.get_env(:live_debugger, :browser_features?) &&
161-
LiveDebugger.Feature.enabled?(:highlighting) do
160+
if LiveDebugger.Feature.enabled?(:highlighting) do
162161
if !socket.assigns.hidden? && socket.assigns.highlight? do
163162
send_event(socket.assigns.lv_process.pid, "highlight", %{attr: attr, val: val})
164163
end

0 commit comments

Comments
 (0)