小能豆

Webview injection javascript

javascript

i have a webview window called blooket, whose code is the following: import webview ;webview.create_window("Blooket", "https://play.blooket.com/play",width=800, height=600, resizable=True, fullscreen=False);webview.start(debug=True). How can i insert javascript code into this window so it supports userscripts?


阅读 64

收藏
2023-12-21

共1个答案

小能豆

If you want to inject JavaScript code into a WebView window created with the webview library in Python, you can use the evaluate_js method provided by the library. This method allows you to execute arbitrary JavaScript code within the WebView window.

Here’s an example of how you can modify your existing code to inject JavaScript code into the WebView window:

import webview

def on_page_loaded():
    # This function will be called when the page is loaded
    # You can inject your JavaScript code here
    javascript_code = """
    // Your userscript JavaScript code goes here
    console.log("Userscript executed!");
    // You can modify the DOM or perform other actions with JavaScript
    """
    webview.evaluate_js(javascript_code)

webview.create_window("Blooket", "https://play.blooket.com/play", width=800, height=600, resizable=True, fullscreen=False, on_loaded=on_page_loaded)
webview.start(debug=True)

In this example, the on_page_loaded function is specified as the on_loaded parameter when creating the window. This function will be called when the page is loaded, and you can inject your userscript JavaScript code within this function using the webview.evaluate_js method.

Replace the placeholder comment // Your userscript JavaScript code goes here with your actual userscript code. The console.log("Userscript executed!"); line is just an example to demonstrate the execution of the userscript.

Make sure to adjust your JavaScript code according to the requirements of your userscript.

2023-12-21