Created
August 12, 2025 02:06
-
-
Save yanyaoer/2f7dd90e79acf6d43df63406e2f46d10 to your computer and use it in GitHub Desktop.
userscript for qutebrowser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S uv run --script | |
# /// script | |
# requires-python = ">=3.12" | |
# dependencies = ["llm", "llm-gemini", "httpx[socks]", "markdownify"] | |
# /// | |
import sys | |
import os | |
import llm | |
import markdownify | |
""" | |
llm userscript for qutebrowser | |
--- | |
Chat context with the selected text or the entire webpage. | |
:spawn --userscript qute-ai {prompt} | |
""" | |
# for someone need proxy | |
# os.environ["all_proxy"] = "socks5://127.0.0.1:1088" | |
fifo = os.getenv("QUTE_FIFO", "/tmp/.qutebrowser.fifo") | |
text = os.getenv("QUTE_SELECTED_TEXT", "") | |
cmd = ( | |
sys.argv[1] if len(sys.argv) > 1 else "summary with input text" | |
) | |
if not text: | |
html = os.getenv("QUTE_HTML") | |
with open(html) as f: | |
text = markdownify.markdownify(f.read()) | |
def log(msg): | |
qute('message-info "{}"'.format(msg)) | |
def qute(cmd): | |
with open(fifo, "w") as f: | |
f.write(cmd) | |
f.write("\n") | |
f.flush() | |
def insert_html(data): | |
script = f""" | |
var div = document.createElement("div"); | |
div.setAttribute("style", "position:fixed; z-index:99999; right:10px; top:10px; background:#fff; padding:10px; width: 300px; height: 500px; overflow: scroll;"); | |
div.innerHTML = "{data}"; | |
div.ondblclick = function() {{ div.parentNode.removeChild(div); }}; | |
document.body.appendChild(div); | |
""" | |
return script.replace("\n", " ") | |
model = llm.get_model("gemini-2.5-flash-lite") | |
response = model.prompt(cmd, system=f"input text is: {text}") | |
# print(cmd, text, model) | |
qute("jseval -q -w main {}".format(insert_html(response.text()))) | |
# log(response.text().replace("\n", " ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment