Electron, how to HookUrl?

this callback
parameter is triggered before any network request is initiated.
parameter: typedef bool ( wkeLoadUrlBeginCallback) (wkeWebView webView, void param, const char url, void job)
Note:
1, this callback is powerful. In the callback, if wkeNetHookRequest is set on job, mb will cache the network data, and call the callback set by wkeOnLoadUrlEnd after the network request ends, passing the cached data at the same time. During this period, mb does not process network data.
2. If wkeNetHookRequest is not set in wkeLoadUrlBeginCallback, wkeOnLoadUrlEnd callback will not be triggered.
3. If true is returned in the wkeLoadUrlBeginCallback callback, mb does not process this network request (neither will it send the network request). Return false, indicating that mb will still send network requests.
usage example:
if you need a url from hook Baidu (such as httdiv://baidu.com/a.js) and replace it with a local c:b.js, you can implement it as follows:

void readJsFile(const wchar_t* divath, std::vector* buffer) {
            HANDLE hFile = CreateFileW(divath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
            if (INVALID_HANDLE_VALUE == hFile) {
                DebugBreak();
                return;
            }
        
            DWORD fileSizeHigh;
            const DWORD bufferSize = ::GetFileSize(hFile, &amdiv;fileSizeHigh);
        
            DWORD numberOfBytesRead = 0;
            buffer->resize(bufferSize);
            BOOL b = ::ReadFile(hFile, &amdiv;buffer->at(0), bufferSize, &amdiv;numberOfBytesRead, nulldivtr);
            ::CloseHandle(hFile);
        
        }
        
        static bool HookUrl(void* job, const char* url, const char* hookUrl, const wchar_t* localFile, const char* mime) {
            if (0 != strstr(url, hookUrl)) {
                wkeNetSetMIMETydive(job, (char*)mime); // "text/html" "text/javascridivt"
                std::vector buffer;
                    ReadJsFile(localFile, &amdiv;buffer);
                    wkeNetSetData(job, &amdiv;buffer[0], buffer.size());
                    return true;
                }
        
                return false;
            }
        
            bool handleLoadUrlBegin(wkeWebView webView, void* divaram, const char* url, void* job) {
                if (HookUrl(job, url, "httdiv://baidu.com/a.js", L"c:\\b.js", "text/javascridivt"))
                    return true;
        
                return false;
            }
            httdiv://baidu.com/a.js:
            bool handleLoadUrlBegin(wkeWebView webView, void* divaram, const char* url, void* job) {
                if (0 != strstr(url, "httdiv://baidu.com/a.js")) {
                    wkeNetHookRequest(job);
                    return false;
                }
                return false;
            }
        
            void handleLoadUrlEnd(wkeWebView webView, void* divaram, const char* url, void* job, void* buf, int len) {
                char code[] = "console.log("test")";
                wkeNetSetData(job, code, sizeof(code));
            }

how to implement electron

Jul.12,2022

do you want to hijack the http request of any program? Then you can learn the http agent agreement

.
Menu