This commit is contained in:
Zengtudor 2025-05-10 18:46:52 +08:00
parent fb77f9e77e
commit baafc2a29d
5 changed files with 209 additions and 6 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/build
/.cache
/.cache
/src/html_string.hpp

View File

@ -3,9 +3,19 @@ cmake_minimum_required(VERSION 3.10)
project(eew CXX)
find_package(webview REQUIRED)
find_package(Threads REQUIRED)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
file(GLOB_RECURSE SRC_CPP CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
file(READ "${CMAKE_CURRENT_LIST_DIR}/src/index.html" HTML_CONTENT CONFIGURE_DEPENDS)
configure_file(
${CMAKE_CURRENT_LIST_DIR}/src/html_string.hpp.in
${CMAKE_CURRENT_LIST_DIR}/src/html_string.hpp
)
add_executable(${PROJECT_NAME} ${SRC_CPP})
target_link_libraries(${PROJECT_NAME} PRIVATE webview::core)
target_link_libraries(${PROJECT_NAME} PRIVATE webview::core Threads::Threads)

5
src/html_string.hpp.in Normal file
View File

@ -0,0 +1,5 @@
#pragma once
constexpr const char* html = R"(
@HTML_CONTENT@
)";

125
src/index.html Normal file
View File

@ -0,0 +1,125 @@
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Environment Variables Table</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-size: 18px;
text-align: left;
}
th, td {
padding: 12px;
border: 1px solid #ddd;
}
th {
background-color: #f4f4f4;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #f1f1f1;
}
@media (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
}
th, td {
text-align: right;
padding: 10px;
}
th {
background-color: #f4f4f4;
position: sticky;
top: 0;
}
td {
border: none;
border-bottom: 1px solid #ddd;
}
td::before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
</style>
</head>
<body>
<h1>Environment Variables</h1>
<table id="envTable">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<!-- Rows will be dynamically inserted here -->
</tbody>
</table>
<script script type="module">
// TODO: async getEnvString() will be implemented elsewhere
// async function refreshEnvTable() {
// try {
// // 调用 getEnvString 函数并解析返回值
// const envString = await window.getEnvString();
// console.log("Environment String:", envString);
// const env = JSON.parse(envString);
// // 清空表格内容
// const tableBody = document.querySelector("#envTable tbody");
// tableBody.innerHTML = "";
// // 填充表格
// for (const [key, value] of Object.entries(env)) {
// const row = document.createElement("tr");
// row.innerHTML = `
// <td data-label="Key">${key}</td>
// <td data-label="Value">${value}</td>
// `;
// tableBody.appendChild(row);
// }
// } catch (error) {
// console.error("Failed to refresh environment table:", error);
// }
// }
setTimeout(async function refreshEnvTable() {
try {
// 调用 getEnvString 函数并解析返回值
const env = await window.getEnvString();
console.log("Environment String:", env);
// 清空表格内容
const tableBody = document.querySelector("#envTable tbody");
tableBody.innerHTML = "";
// 填充表格
for (const [key, value] of Object.entries(env)) {
const row = document.createElement("tr");
row.innerHTML = `
<td data-label="Key">${key}</td>
<td data-label="Value">${value}</td>
`;
tableBody.appendChild(row);
}
} catch (error) {
console.error("Failed to refresh environment table:", error);
}
}, 1000);
// 页面加载时刷新表格
// document.addEventListener("DOMContentLoaded", refreshEnvTable);
</script>
</body>
</html>

View File

@ -1,9 +1,71 @@
#include "html_string.hpp"
#include <cstddef>
#include <cstdlib>
#include <exception>
#include <format>
#include <iostream>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <webview/backends.hh>
#include <webview/webview.h>
extern char** environ;
int main(){
webview::webview wv(false,nullptr);
wv.navigate("https://www.baidu.com");
wv.run();
using vpss_t = std::vector<std::pair<std::string, std::string>>;
vpss_t getEnvKv(){
vpss_t result;
for(char **env = environ; *env != nullptr; env++){
const std::string thisEnv = *env;
const size_t idx = thisEnv.find('=');
if (idx == std::string::npos) {
throw std::logic_error("cannot parse env");
}
result.emplace_back(thisEnv.substr(0,idx),thisEnv.substr(idx+1));
}
return result;
}
template<class T,class U>
std::ostream&operator<<(std::ostream &os,const std::pair<T,U> &p){
os<<"{ "<<p.first<<", "<<p.second<<" }";
return os;
}
template<class T>
std::ostream&operator<<(std::ostream &os,const std::vector<T> &v){
os<<"[ ";
if(v.size()==1){
os<<v[0]<<" ]";
return os;
}
for(size_t i=0;i<v.size()-1;i++){
os<<v[i]<<", ";
}
os<<" ]";
return os;
}
int main(const int argc,char *argv[],char *env[]){
try {
std::cout<<getEnvKv()<<'\n';
webview::webview wv(true,nullptr);
wv.set_html(html);
wv.bind("getEnvString",
[](const std::string &req)->std::string {
std::stringstream oss;
oss<<getEnvKv();
std::cout<<std::format("{{env:\"{}\"}}",getEnvKv()[0].first)<<'\n';
return std::format("{{\"env\":\"{}\"}}",getEnvKv()[0].first);
});
wv.run();
} catch (const std::exception &e) {
std::cerr<<e.what()<<'\n';
return 1;
}
}