Skip to content

Commit

Permalink
scripting advancements
Browse files Browse the repository at this point in the history
  • Loading branch information
aloneguid committed Aug 16, 2024
1 parent b34b0bb commit b6e87cb
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 108 deletions.
15 changes: 15 additions & 0 deletions bt/app/script_site.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "script_site.h"
#include <fstream>
#include <regex>

using namespace std;

Expand Down Expand Up @@ -98,4 +99,18 @@ namespace bt {

return r;
}

std::vector<std::string> script_site::list_function_names() {
vector<string> r;

// use regex to list function names
regex re{R"(function\s+(\w+))"};
sregex_iterator it{code.begin(), code.end(), re};
sregex_iterator end;
for(; it != end; ++it) {
r.push_back(it->str(1));
}

return r;
}
}
7 changes: 7 additions & 0 deletions bt/app/script_site.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "lua.hpp"
#include <string>
#include <vector>
#include "click_payload.h"

namespace bt {
Expand All @@ -21,6 +22,12 @@ namespace bt {
// bt specific functions
bool call_rule(click_payload& up, const std::string& function_name);

/**
* @brief Analyse the code and return a list of Lua function names
* @return
*/
std::vector<std::string> list_function_names();


private:
bool is_path;
Expand Down
5 changes: 5 additions & 0 deletions bt/app/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

namespace bt {
const std::string PipelineDebugger{"Pipeline debugger"};
const std::string ChromiumBased{"Chromium based"};
const std::string GeckoBased{"Gecko based"};

const std::string MenuTools{"Tools"};
const std::string ScriptEditor{"Script editor"};
}
143 changes: 48 additions & 95 deletions bt/app/ui/config_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace bt::ui {
wnd_config{title, &is_open},
wnd_about{"About"},
wnd_subs{"Substitutions", &show_subs},
wnd_scripting{"Scripting", &show_scripting},
wnd_scripting{ScriptEditor, &show_scripting},
wnd_pv{PipelineDebugger, &pv_show} {

app = grey::app::make(title, 900, 500);
Expand Down Expand Up @@ -116,10 +116,6 @@ namespace bt::ui {
if(g_config.browsers.empty()) {
render_no_browsers();
} else {
if (show_url_tester) {
render_url_tester_input();
}

render_browsers();
}

Expand Down Expand Up @@ -191,7 +187,7 @@ namespace bt::ui {
}
}

if(w::menu m_tools{"Tools"}; m_tools) {
if(w::menu m_tools{MenuTools}; m_tools) {
if(w::mi("Open Windows Defaults", true, ICON_MD_PSYCHOLOGY)) {
win32::shell::open_default_apps();
}
Expand All @@ -201,6 +197,9 @@ namespace bt::ui {
if(w::mi(PipelineDebugger, true, ICON_MD_DIRECTIONS_RUN)) {
pv_show = !pv_show;
}
if(w::mi(ScriptEditor, true, ICON_MD_CODE)) {
show_scripting = !show_scripting;
}
if(w::menu m{"Troubleshooting", true}; m) {
if(w::mi("Re-register custom protocol")) {
w::notify_info("todo");
Expand Down Expand Up @@ -261,9 +260,6 @@ namespace bt::ui {
if(w::mi("Substitutions...", true, ICON_MD_FIND_REPLACE)) {
show_subs = !show_subs;
}
if(w::mi("Scripting...", true, ICON_MD_CODE)) {
show_scripting = !show_scripting;
}
}

if(w::menu m{"Help"}; m) {
Expand Down Expand Up @@ -517,77 +513,57 @@ It super fast, extremely light on resources, completely free and open source.)",
}
}

void config_app::render_url_tester_input() {

w::container c{"url_tester", 0, 65 * app->scale};
c.border();
w::guard g{c};

//bool different = url_tester_up.url != url_tester_up.open_url;
bool different = false;
float i0w = (different ? 350 : 500) * app->scale;

bool i0 = w::input(url_tester_up.url, ICON_MD_LINK " URL", true, i0w);
//w::tooltip(fmt::format("match on: {}\nopen: {}", url_tester_up.match_url, url_tester_up.open_url));

//if(different) {
// w::sl();
// w::input(url_tester_up.open_url, ICON_MD_OPEN_IN_NEW, true, 350 * app->scale, true);
// w::tooltip("URL that will be opened in the browser");
//}

w::sl(800 * app->scale);
if (w::button(ICON_MD_CLEAR_ALL " clear", w::emphasis::error)) {
url_tester_up.clear();
}
bool i1 = w::input(url_tester_up.window_title, ICON_MD_WINDOW " window", true, 301 * app->scale);
w::tooltip("window title");
w::sl();
bool i2 = w::input(url_tester_up.process_name, ICON_MD_MEMORY " process", true, 120 * app->scale);
w::tooltip("process name");

//w::sl();
//w::label(std::to_string(url_tester_payload_version));

if(i0 || i1 || i2) {
test_url();
}
}

void config_app::render_scripting_window() {
w::guard gw{wnd_scripting};

{
w::tab_bar tabs{"scriptTabs"};
if(tabs.next_tab("Pipeline step")) {
if(!script_initialised) {
script_editor.set_text(g_script.get_code());
script_initialised = true;
}

if(!g_script.get_error().empty()) {
w::label(g_script.get_error(), w::emphasis::error);
}
if(!script_initialised) {
script_editor.set_text(g_script.get_code());
script_fns = g_script.list_function_names();
script_initialised = true;
}

if(w::accordion("Test")) {
w::input(scripting_up.url, ICON_MD_LINK " URL", true);
w::input(scripting_up.window_title, ICON_MD_WINDOW " window", true);
w::input(scripting_up.process_name, ICON_MD_MEMORY " process", true);
}
if(!g_script.get_error().empty()) {
w::container c{"err", 0, 0};
c.border().auto_size_y();
w::guard g{c};
w::label(g_script.get_error(), w::emphasis::error);
}

if(w::button(ICON_MD_SAVE " save", w::emphasis::primary)) {
g_script.set_code(script_editor.get_text());
}
w::input(g_config.pv_last_url, ICON_MD_LINK " URL", true);
w::input(g_config.pv_last_wt, ICON_MD_WINDOW " window", true);
w::input(g_config.pv_last_pn, ICON_MD_MEMORY " process", true);

script_editor.render();
w::combo("##fn", script_fns, script_fn_selected, 150);
w::tooltip("function to execute");
w::sl();
if(w::button(ICON_MD_PLAY_ARROW)) {

g_script.set_code(script_editor.get_text());
if(g_script.get_error().empty()) {
// test it
click_payload up;
up.url = g_config.pv_last_url;
up.window_title = g_config.pv_last_wt;
up.process_name = g_config.pv_last_pn;
g_script.call_rule(up, script_fns[script_fn_selected]);
}
}
w::tooltip("run the selected function");
w::sl();
if(w::button(ICON_MD_REFRESH)) {
script_fns = g_script.list_function_names();
}
w::tooltip("refresh list of functions");

if(tabs.next_tab("Rules")) {
w::sl();
w::label("|", 0, false);

}
w::sl();
if(w::button(ICON_MD_SAVE " save", w::emphasis::primary)) {
g_script.set_code(script_editor.get_text());
}


script_editor.render();
}

void config_app::render_pipe_visualiser_window() {
Expand Down Expand Up @@ -839,9 +815,6 @@ It super fast, extremely light on resources, completely free and open source.)",
w::sl();
w::icon_checkbox(ICON_MD_VISIBILITY, g_config.show_hidden_browsers);
w::tooltip("Show hidden browsers");
w::sl();
w::icon_checkbox(ICON_MD_CRUELTY_FREE, show_url_tester);
w::tooltip("Show testing bar");

for(int i = 0; i < g_config.browsers.size(); i++) {
auto br = g_config.browsers[i];
Expand Down Expand Up @@ -912,12 +885,6 @@ It super fast, extremely light on resources, completely free and open source.)",
w::move_pos(left_pad, 0);
w::label(b->name);

if(!url_tester_up.empty()) {
w::sl();
w::label(b->ui_test_url_matches ? RuleMatchesIcon : RuleDoesNotMatchIcon,
b->ui_test_url_matches ? w::emphasis::primary : w::emphasis::error);
}

w::move_pos(left_pad, 0);

if(b->instances.size() > 0) {
Expand All @@ -929,13 +896,11 @@ It super fast, extremely light on resources, completely free and open source.)",
if(b->is_chromium) {
w::sl();
w::icon_image(*app, "bt_chromium");
//w::label(ICON_MD_TAB, 0, false);
w::tooltip("Chromium-based");
w::tooltip(ChromiumBased);
} else if(b->is_firefox) {
w::sl();
w::icon_image(*app, "bt_gecko");
//w::label(ICON_MD_WHATSHOT, 0, false);
w::tooltip("Firefox-based");
w::tooltip(GeckoBased);
}
} else {
w::label(ICON_MD_SUPPORT_AGENT, 0, false);
Expand Down Expand Up @@ -1047,7 +1012,7 @@ It super fast, extremely light on resources, completely free and open source.)",
string tab_title = fmt::format(" {}{} ", tab_icon, bi->name);

{
auto t = tabs.next_tab(tab_title, bi->ui_test_url_matches);
auto t = tabs.next_tab(tab_title);
if(t) {
selected_profile_idx = idx;
w::spc();
Expand Down Expand Up @@ -1233,13 +1198,6 @@ special keyword - %url% which is replaced by opening url.)");
for(int i = 0; i < bi->rules.size(); i++) {
auto rule = bi->rules[i];

if(!url_tester_up.empty()) {
bool is_match = rule->is_match(url_tester_up);
w::label(is_match ? RuleMatchesIcon : RuleDoesNotMatchIcon,
is_match ? w::emphasis::primary : w::emphasis::error);
w::sl();
}

// location
w::combo(string{"##loc"} + std::to_string(i),
rule_locations, (size_t&)rule->loc, 70 * app->scale);
Expand Down Expand Up @@ -1314,11 +1272,6 @@ special keyword - %url% which is replaced by opening url.)");
}
}

void config_app::test_url() {
url_tester_up.url = "";
g_pipeline.process(url_tester_up);
}

void config_app::recalculate_test_url_matches(const click_payload& cp) {
for(auto b : g_config.browsers) {
b->ui_test_url_matches = false;
Expand Down
10 changes: 3 additions & 7 deletions bt/app/ui/config_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ namespace bt::ui {
bool script_initialised{false};
grey::widgets::window wnd_scripting{"Scripting"};
grey::widgets::text_editor script_editor;
click_payload scripting_up;

// URL Tester
bool show_url_tester{false};
click_payload url_tester_up;
std::vector<std::string> script_fns;
size_t script_fn_selected;
bool script_fn_result{false};

// Pipe visualiser window
bool pv_show{false};
Expand All @@ -98,7 +96,6 @@ namespace bt::ui {
void render_about_window();
void render_subs_window();
void render_dashboard();
void render_url_tester_input();
void render_scripting_window();
void render_pipe_visualiser_window();

Expand All @@ -119,7 +116,6 @@ namespace bt::ui {

void rediscover_browsers();
void add_custom_browser_by_asking();
void test_url();

void recalculate_test_url_matches(const click_payload& cp);

Expand Down
8 changes: 4 additions & 4 deletions bt/bt.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ IDI_ICON1 ICON "icon.ico"
IDI_ICON2 ICON "pdf.ico"

1 VERSIONINFO
FILEVERSION 4,0,0,0
PRODUCTVERSION 4,0,0,0
FILEVERSION 5,0,0,0
PRODUCTVERSION 5,0,0,0
FILEFLAGSMASK 0x3fL
FILEFLAGS 0x0L
FILEOS 0x40004L
Expand All @@ -16,12 +16,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Ivan Gavryliuk"
VALUE "FileDescription", "Browser Tamer"
VALUE "FileVersion", "4.0.0.0"
VALUE "FileVersion", "5.0.0.0"
VALUE "InternalName", "Browser Tamer"
VALUE "LegalCopyright", "Copyright (C) 2024 by Ivan Gavryliuk"
VALUE "OriginalFilename", "bt.exe"
VALUE "ProductName", "Browser Tamer"
VALUE "ProductVersion", "4.0.0.0"
VALUE "ProductVersion", "5.0.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
15 changes: 15 additions & 0 deletions bt/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by bt.rc
//

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
1 change: 1 addition & 0 deletions bt/template.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Browser Tamer Lua scripting starter script
4 changes: 2 additions & 2 deletions pre-build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ $v2 = $version + ".0"

# update version in bt.rc
(Get-Content .\bt\bt.rc) `
-replace "4,\d+,\d+,\d+", "$v1" |
-replace "5,\d+,\d+,\d+", "$v1" |
Out-File .\bt\bt.rc
(Get-Content .\bt\bt.rc) `
-replace "4\.\d+\.\d+\.\d+", "$v2" |
-replace "5\.\d+\.\d+\.\d+", "$v2" |
Out-File .\bt\bt.rc

0 comments on commit b6e87cb

Please sign in to comment.