File size: 8,663 Bytes
327d208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a324f3
327d208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a324f3
327d208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34d4681
959f2b1
 
ad1e294
959f2b1
ad1e294
 
 
 
959f2b1
34d4681
4db63fb
327d208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.getElementById('keyword-input');
    const limitInput = document.getElementById("limit-input");
    const searchButton = document.getElementById('search-button');
    const limitCheckbox = document.getElementById("limit-check");
    const popupText = document.querySelector(".scrollable-text");
    const popupTitle = document.getElementById("popup-title");
    const body = document.body;
    const resultsContainer = document.getElementById('results-container');

    // Function to perform the search
    async function performSearch() {
        const keyword = searchInput.value.trim();
        let limit = limitInput.value.trim();
        if (!keyword) {
            resultsContainer.innerHTML = '<div class="error-message">Please enter a search keyword.</div>';
            return;
        }

        if (!limit){
            limit = 15;
        }

        // Show loading indicator
        resultsContainer.innerHTML = '<div class="loading">Searching for ' + limit.toString() + ' papers about "' + keyword + '"...</div>';

        try {
            const response = await fetch('https://om4r932-arxiv.hf.space/search', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    keyword: keyword,
                    limit: limit
                })
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.json();
            
            if (data.error) {
                resultsContainer.innerHTML = `<div class="error-message">Search error: ${data.message}</div>`;
                return;
            }

            displayResults(data.message);
        } catch (error) {
            console.error('Search error:', error);
            resultsContainer.innerHTML = `<div class="error-message">Error performing search: ${error.message}</div>`;
        }
    }

    function handleCustomLimit(){
        if(limitCheckbox.checked){
            limitInput.removeAttribute("disabled")
        } else {
            limitInput.value = 15;
            limitInput.setAttribute("disabled", "")
        }
    }

    // Function to display the results
    function displayResults(results) {
        resultsContainer.innerHTML = '';
        
        if (!results || Object.keys(results).length === 0) {
            resultsContainer.innerHTML = '<div class="error-message">No results found. Try a different keyword.</div>';
            return;
        }

        const resultsCount = Object.keys(results).length;
        const resultsHeader = document.createElement('h2');
        resultsHeader.textContent = `Found ${resultsCount} paper${resultsCount > 1 ? 's' : ''}`;
        resultsContainer.appendChild(resultsHeader);

        Object.entries(results).forEach(([id, paper]) => {
            const paperElement = document.createElement('div');
            paperElement.classList.add('paper-result');
            
            // Format the abstract to preserve line breaks
            const formattedAbstract = paper.abstract;
            
            paperElement.innerHTML = `
                <div class="paper-title">${paper.title}</div>
                <div class="paper-authors">${paper.authors}</div>
                <div class="paper-date">Published: ${paper.date}</div>
                <div class="paper-abstract">${formattedAbstract}</div>
                <div class="paper-id">ArXiv ID: ${id}</div>
                <a type="button" class="btn btn-primary" role="button" href="${paper.pdf}">Show PDF</a>
                <input type="button" value="Extract Text" doc="${id}" class="extractText btn btn-success"/>
                <input type="button" value="Get titles" doc="${id}" class="getTitle btn btn-danger"/>
            `;
            resultsContainer.appendChild(paperElement);
        });

        document.querySelectorAll(".extractText").forEach(btn => {
            btn.addEventListener("click", async function () {
                let id_doc = btn.getAttribute("doc");
                popupTitle.textContent = `PDF extraction - Document no ${id_doc}`;
                try {
                    const response = await fetch('https://om4r932-arxiv.hf.space/extract_pdf/arxiv_id', {
                        method: 'POST',
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            doc_id: id_doc
                        })
                    });
        
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
        
                    const data = await response.json();
                    
                    if (data.error) {
                        popupText.innerHTML = `<div class="error-message">Search error: ${data.message}</div>`;
                        return;
                    }
                    
                    popupText.textContent = data.text;
                } catch (error) {
                    console.error('Search error:', error);
                    popupText.innerHTML = `<div class="error-message">Error performing search: ${error.message}</div>`;
                }
                document.getElementById("popup").style.display = "flex";
                body.classList.add("no-scroll")
            });
        })

        document.querySelectorAll(".getTitle").forEach(btn => {
            btn.addEventListener("click", async function () {
                let id_doc = btn.getAttribute("doc");
                popupTitle.textContent = `Chapters - Document no ${id_doc}`;
                try {
                    const response = await fetch('https://om4r932-arxiv.hf.space/extract_pdf/arxiv_id', {
                        method: 'POST',
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            doc_id: id_doc
                        })
                    });
        
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
        
                    const data = await response.json();
                    
                    if (data.error) {
                        popupText.innerHTML = `<div class="error-message">Search error: ${data.message}</div>`;
                        return;
                    }
                    let t = "";
                    if(Array.isArray(data.titles)){
                        for(const [lvl, title] of data.titles){
                            if(lvl == 1){
                                t += title + "<br>"
                            } else if (lvl == 2) {
                                t += "<pre>" + title + "<br></pre>"
                            } else {
                                t += title + "<br>"
                            }
                        }
                        popupText.innerHTML = t;  
                    } else {
                        popupText.textContent = data.titles;
                    }
                } catch (error) {
                    console.error('Search error:', error);
                    popupText.innerHTML = `<div class="error-message">Error performing search: ${error.message}</div>`;
                }
                document.getElementById("popup").style.display = "flex";
                body.classList.add("no-scroll")
            });
        })
    }

    // Add event listeners
    document.querySelector(".close").addEventListener("click", () => {
        document.getElementById("popup").style.display = "none";
        body.classList.remove("no-scroll");
    })

    window.addEventListener("click", function (event) {
        if (event.target === popup) {
            popup.style.display = "none";
            body.classList.remove("no-scroll");
        }
    });

    searchButton.addEventListener('click', performSearch);
    limitCheckbox.addEventListener("change", handleCustomLimit);
    searchInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') performSearch();
    });
});