forked from gold-silver-copper/egui-macroquad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad-url.js
85 lines (81 loc) · 2.84 KB
/
quad-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var ctx = null;
var memory;
params_set_mem = function (wasm_memory, _wasm_exports) {
memory = wasm_memory;
ctx = {};
}
function set_url(params, hash) {
let result = window.location.origin + window.location.pathname;
if (params != "") {
if (params !== undefined && params !== null) {
result += '?' + params;
} else {
result += window.location.search;
}
}
if (hash != "") {
if (hash !== undefined && hash !== null) {
result += '#' + hash;
} else {
result += window.location.hash;
}
}
window.history.pushState({path:result},'',result); // https://stackoverflow.com/questions/10970078/modifying-a-query-string-without-reloading-the-page
}
params_register_js_plugin = function (importObject) {
importObject.env.quad_url_path = function (full) {
if (full == 1) {
return js_object(window.location.href);
} else {
return js_object(window.location.origin + window.location.pathname);
}
}
importObject.env.quad_url_param_count = function () {
ctx.entries = [];
var some = new URLSearchParams(window.location.search);
for (let i of some.entries()) {
ctx.entries.push(i);
}
return ctx.entries.length;
}
importObject.env.quad_url_get_key = function (i) {
return js_object(ctx.entries[i][0])
}
importObject.env.quad_url_get_value = function (i) {
return js_object(ctx.entries[i][1])
}
importObject.env.quad_url_link_open = function (url_rs, new_tab) {
let url = get_js_object(url_rs);
if (new_tab == 0) {
window.open(url, "_self"); // https://stackoverflow.com/questions/8454510/open-url-in-same-window-and-in-same-tab
} else {
window.open(url);
}
}
importObject.env.quad_url_set_program_parameter = function (name_rs, value_rs) {
let name = get_js_object(name_rs);
let value = get_js_object(value_rs);
let params = new URLSearchParams(window.location.search);
params.set(name, value);
// todo убрать вопрос если пусто
set_url(params.toString(), null);
}
importObject.env.quad_url_delete_program_parameter = function (name_rs) {
let name = get_js_object(name_rs);
let params = new URLSearchParams(window.location.search);
params.delete(name);
set_url(params.toString(), null);
}
importObject.env.quad_url_get_hash = function () {
return js_object(window.location.hash);
}
importObject.env.quad_url_set_hash = function (hash) {
set_url(null, get_js_object(hash));
}
}
miniquad_add_plugin({
register_plugin: params_register_js_plugin,
on_init: params_set_mem,
name: "quad_url",
version: "0.1.1"
});