File size: 2,004 Bytes
457bc51
e4176ce
457bc51
e4176ce
 
457bc51
 
e4176ce
 
 
 
 
457bc51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4176ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457bc51
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
import streamlit_ace as ace
import base64
import os
import time

def get_clipboard_html():
    script_path = os.path.abspath(__file__)
    script_dir = os.path.dirname(script_path)
    clipboard_path = os.path.join(script_dir, "clipboard.html")
    
    with open(clipboard_path, "r") as f:
        content = f.read()
        b64 = base64.b64encode(content.encode()).decode()
        return f"data:text/html;base64,{b64}"

def main():
    st.title("Clipboard Monitor")

    st.write("Paste your data in the input box below:")

    clipboard_data = st.empty()
    data_type = st.empty()
    data_contents = st.empty()

    st.write(f'<iframe src="{get_clipboard_html()}" width="100%" height="300" style="border:none"></iframe>', unsafe_allow_html=True)

    # Set up session state
    if "last_clipboard" not in st.session_state:
        st.session_state.last_clipboard = ""

    while True:
        event = st.experimental_get_query_params().get("event", [None])[0]
        if event == "clipboard-update":
            st.experimental_set_query_params(event="")
            clipboard_text = st.experimental_get_query_params().get("data", [""])[0]
            clipboard_text = base64.urlsafe_b64decode(clipboard_text).decode(errors="ignore")

            if clipboard_text != st.session_state.last_clipboard:
                st.session_state.last_clipboard = clipboard_text
                clipboard_data.write(f"Clipboard Data: {clipboard_text}")

                if clipboard_text.startswith("{") and clipboard_text.endswith("}"):
                    data_type.write("Data Type: JSON")
                elif clipboard_text.startswith("<") and clipboard_text.endswith(">"):
                    data_type.write("Data Type: XML/HTML")
                else:
                    data_type.write("Data Type: Plain Text")

                data_contents.write(f"Data Contents: {clipboard_text}")

        time.sleep(1)
        st.experimental_rerun()

if __name__ == "__main__":
    main()