I’ve been working on a Streamlit app where I want to capture click signals when a user clicks a link within a PDF displayed using Streamlit-Python-Viewer. I want to get the content of the signal and also suppress the event. I’ve tried injecting a javascript addEventListener like this:
import streamlit as st
from streamlit_pdf_reader import pdf_reader
def main():
st.title("PDF Click Signals Demo")
# Upload a PDF file
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
if uploaded_file is not None:
pdf_reader(uploaded_file)
# Inject JavaScript to capture click signals
st.markdown(
"""
<script>
document.addEventListener("click", function(event) {
if (event.target.tagName.toLowerCase() === "a") {
// Send click signal to Streamlit
var href = event.target.getAttribute("href");
if (href.startsWith("http")) {
// Send signal if the link is an external URL
window.parent.postMessage({ type: "pdf_link_clicked", href: href }, "*");
}
}
});
</script>
""",
unsafe_allow_html=True
)
if __name__ == "__main__":
main()
But now the pdf is not even displayed. Does anybody have an idea on how to get the signal, when a link is clicked.