Domain 判斷 使用wasm

簡單的判斷domain是否為本站

C++ 程式碼

#include <emscripten/val.h>
#include <string>

// 使用 extern "C" 確保函數名稱不被修改
extern "C" {
    int is_quantoyo() {
        emscripten::val location = emscripten::val::global("location");
        std::string hostname = location["hostname"].as<std::string>();
        return hostname == "tool.quantoyo.com" ? 1 : 0;
    }
}

編譯與使用

編譯
emcc main.cpp -o quantoyo.js -sEXPORTED_FUNCTIONS="['_is_quantoyo']" -sEXPORTED_RUNTIME_METHODS="['ccall']" --bind -std=c++11

使用
1. 將產生的 quantoyo.js 、quantoyo.wams 跟自己寫的index_quanto.html 放到網頁伺服器上打開即可
2. demo: https://tools.quantoyo.com/domain/index_quanto.html

附上emsdk使用方法

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ~/emsdk/emsdk_env.sh
emcc --version

可以看到版號就算安裝成功

最後感謝Chat GPT 大力協助…

發佈留言