Skip to content

Commit 77d2b29

Browse files
authored
Merge pull request #15 from NSTechBytes/working
Working
2 parents 90799bf + 3531b2d commit 77d2b29

File tree

14 files changed

+422
-328
lines changed

14 files changed

+422
-328
lines changed

WebView2/Extension.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
4+
5+
#include "Extension.h"
6+
#include "Plugin.h"
7+
#include "../API/RainmeterAPI.h"
8+
#include <sstream>
9+
10+
bool g_extensions_checked = false;
11+
12+
std::vector<std::wstring> GetExtensionsID(const std::wstring& input)
13+
{
14+
std::vector<std::wstring> result;
15+
std::wstringstream ss(input);
16+
std::wstring token;
17+
18+
while (std::getline(ss, token, L',')) {
19+
token.erase(0, token.find_first_not_of(L" \t"));
20+
token.erase(token.find_last_not_of(L" \t") + 1);
21+
22+
if (!token.empty()) {
23+
result.push_back(token);
24+
}
25+
}
26+
return result;
27+
}
28+
29+
void EnableExtension(ICoreWebView2BrowserExtension* extension, BOOL enable)
30+
{
31+
extension->Enable(
32+
enable,
33+
Callback<ICoreWebView2BrowserExtensionEnableCompletedHandler>(
34+
[](HRESULT hr) -> HRESULT
35+
{
36+
if (FAILED(hr))
37+
ShowFailure(hr, L"Enable extension failed");
38+
return S_OK;
39+
}).Get());
40+
}
41+
42+
void RemoveExtension(void* rm, ICoreWebView2BrowserExtension* extension, const std::wstring& name)
43+
{
44+
extension->Remove(
45+
Callback<ICoreWebView2BrowserExtensionRemoveCompletedHandler>(
46+
[](HRESULT hr) -> HRESULT
47+
{
48+
if (FAILED(hr))
49+
ShowFailure(hr, L"Uninstall extension failed");
50+
return S_OK;
51+
}).Get());
52+
53+
RmLogF(rm, LOG_DEBUG, L"WebView2: \"%s\" extension removed.", name.c_str());
54+
}

WebView2/Extension.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
4+
5+
#pragma once
6+
7+
#include <Windows.h>
8+
#include <WebView2.h>
9+
#include <string>
10+
#include <vector>
11+
12+
// Global flag for extension checking
13+
extern bool g_extensions_checked;
14+
15+
// Extension utility functions
16+
std::vector<std::wstring> GetExtensionsID(const std::wstring& input);
17+
void EnableExtension(ICoreWebView2BrowserExtension* extension, BOOL enable);
18+
void RemoveExtension(void* rm, ICoreWebView2BrowserExtension* extension, const std::wstring& name);

WebView2/HostObject.idl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Copyright (C) 2025 nstechbytes. All rights reserved.
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
24

35
import "oaidl.idl";
46
import "ocidl.idl";

WebView2/HostObjectRmAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (C) 2025 nstechbytes. All rights reserved.
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
33
*/
44

55
#include "HostObjectRmAPI.h"

WebView2/HostObjectRmAPI.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**
2-
* Copyright (C) 2025 nstechbytes. All rights reserved.
3-
*/
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
44

55
#pragma once
66
#include "HostObject_h.h"

WebView2/PathUtils.cpp

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
4+
5+
#include "PathUtils.h"
6+
#include "Utils.h"
7+
#include "../API/RainmeterAPI.h"
8+
#include <vector>
9+
10+
// Path and URI utilities
11+
std::wstring NormalizeUri(const std::wstring& uri)
12+
{
13+
const std::wstring scheme_sep = L"://";
14+
auto scheme_pos = uri.find(scheme_sep);
15+
if (scheme_pos == std::wstring::npos)
16+
return uri;
17+
18+
const std::wstring scheme = uri.substr(0, scheme_pos);
19+
const size_t after_scheme = scheme_pos + scheme_sep.length();
20+
21+
if (scheme == L"file")
22+
{
23+
size_t last_slash = uri.find_last_of(L'/');
24+
if (last_slash != std::wstring::npos)
25+
{
26+
return uri.substr(0, last_slash + 1);
27+
}
28+
return uri;
29+
}
30+
31+
size_t path_start = uri.find(L'/', after_scheme);
32+
if (path_start == std::wstring::npos)
33+
{
34+
return uri + L"/";
35+
}
36+
37+
return uri.substr(0, path_start + 1);
38+
}
39+
40+
std::wstring NormalizePath(void* rm, LPCWSTR path)
41+
{
42+
if (!path || !*path)
43+
return {};
44+
45+
std::wstring value = path;
46+
47+
// Relative path - absolute
48+
if (value[0] != L'/' && (value.length() < 2 || value[1] != L':'))
49+
{
50+
if (LPCWSTR absolutePath = RmPathToAbsolute(rm, value.c_str()))
51+
{
52+
value = absolutePath;
53+
}
54+
}
55+
56+
// Normalize slashes
57+
for (wchar_t& ch : value)
58+
{
59+
if (ch == L'\\') ch = L'/';
60+
}
61+
62+
// Enforce extension if required
63+
auto dotPos = value.find_last_of(L'.');
64+
if (dotPos == std::wstring::npos)
65+
{
66+
RmLog(rm, LOG_ERROR, L"Execute: File extension is missing, use '.js'.");
67+
return {};
68+
}
69+
70+
std::wstring extension = value.substr(dotPos);
71+
for (wchar_t& ch : extension)
72+
ch = towlower(ch);
73+
74+
if (_wcsicmp(extension.c_str(), L".js") != 0)
75+
{
76+
std::wstring msg = L"Execute: The file extension '";
77+
msg.append(extension);
78+
msg += L"' is not supported. Use '.js' extension.";
79+
RmLog(rm, LOG_ERROR, msg.c_str());
80+
return {};
81+
}
82+
return value;
83+
}
84+
85+
bool IsFilePathSyntax(LPCWSTR input)
86+
{
87+
if (!input || input[0] == L'\0') return false;
88+
89+
bool hasExtension = false;
90+
bool hasSeparator = false;
91+
92+
const wchar_t* lastDot = nullptr;
93+
const wchar_t* p = input;
94+
95+
while (*p) {
96+
if (wcschr(L"<>:\"|?*();'", *p)) {
97+
if (!(*p == L':' && p == input + 1)) {
98+
return false;
99+
}
100+
}
101+
102+
if (*p == L'\\' || *p == L'/') {
103+
hasSeparator = true;
104+
lastDot = nullptr;
105+
}
106+
else if (*p == L'.') {
107+
lastDot = p;
108+
}
109+
p++;
110+
}
111+
112+
if (lastDot != nullptr && lastDot != input) {
113+
if (!iswspace(*(lastDot + 1)) && *(lastDot + 1) != L'\0') {
114+
hasExtension = true;
115+
}
116+
}
117+
118+
bool hasSpace = (wcschr(input, L' ') != nullptr);
119+
120+
if (hasSpace && !hasSeparator) {
121+
return false;
122+
}
123+
124+
return hasSeparator || hasExtension;
125+
}
126+
127+
// File reading utilities
128+
std::wstring ReadScriptFile(const std::wstring& path)
129+
{
130+
std::ifstream is(path, std::ios::binary);
131+
if (!is) {
132+
throw std::runtime_error("Failed to open file");
133+
}
134+
135+
// Read all bytes
136+
std::vector<char> bytes((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
137+
size_t n = bytes.size();
138+
if (n == 0) return std::wstring();
139+
140+
const unsigned char* ub = reinterpret_cast<const unsigned char*>(bytes.data());
141+
142+
// Detect BOMs
143+
if (n >= 3 && ub[0] == 0xEFu && ub[1] == 0xBBu && ub[2] == 0xBFu) {
144+
// UTF-8 with BOM -> skip BOM then convert
145+
return Utf8ToWstring(reinterpret_cast<const char*>(ub + 3), static_cast<int>(n - 3));
146+
}
147+
148+
if (n >= 2 && ub[0] == 0xFFu && ub[1] == 0xFEu) {
149+
// UTF-16 LE with BOM
150+
std::wstring out;
151+
out.reserve((n - 2) / 2);
152+
for (size_t i = 2; i + 1 < n; i += 2) {
153+
wchar_t ch = static_cast<wchar_t>(ub[i] | (ub[i + 1] << 8));
154+
out.push_back(ch);
155+
}
156+
return out;
157+
}
158+
159+
if (n >= 2 && ub[0] == 0xFEu && ub[1] == 0xFFu) {
160+
// UTF-16 BE with BOM -> swap bytes
161+
std::wstring out;
162+
out.reserve((n - 2) / 2);
163+
for (size_t i = 2; i + 1 < n; i += 2) {
164+
wchar_t ch = static_cast<wchar_t>((ub[i] << 8) | ub[i + 1]);
165+
out.push_back(ch);
166+
}
167+
return out;
168+
}
169+
170+
// No BOM: assume UTF-8 and convert
171+
return Utf8ToWstring(reinterpret_cast<const char*>(ub), static_cast<int>(n));
172+
}

WebView2/PathUtils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
4+
5+
#pragma once
6+
7+
#include <Windows.h>
8+
#include <string>
9+
#include <fstream>
10+
11+
// Path and URI utilities
12+
std::wstring NormalizeUri(const std::wstring& uri);
13+
std::wstring NormalizePath(void* rm, LPCWSTR path);
14+
bool IsFilePathSyntax(LPCWSTR input);
15+
16+
// File reading utilities
17+
std::wstring ReadScriptFile(const std::wstring& path);

0 commit comments

Comments
 (0)