PageSourceSearch

https://xhdeng19.github.io/assets/js/semantic_scholar_citation_count.js

js xhdeng19.github.io collected 2026-10-03 10:11:24 UTC 2,759 bytes, 68 lines download raw bytes

1const citationCountElements = document.querySelectorAll('[data-semantic-scholar-id]');
2citationCountElements.forEach(element => {
3    const id = element.getAttribute('data-semantic-scholar-id');
4    if (id) {
5        element.setAttribute('data-semantic-scholar-id', id.toLowerCase());
6    }
7});
8
9const semanticScholarIds = new Set(Array.from(citationCountElements).map(element => element.getAttribute('data-semantic-scholar-id')).filter(id => id));
10
11let uncachedSemanticScholarIds = [];
12semanticScholarIds.forEach(id => {
13    const cacheKey = `semanticScholarCitationCount:${id}`;
14    const cachedData = localStorage.getItem(cacheKey);
15    if (cachedData) {
16        const { _, timestamp } = JSON.parse(cachedData);
17        // If cached data is older than 1 hour, consider it uncached
18        if (Date.now() - timestamp > 1 * 60 * 60 * 1000) {
19            uncachedSemanticScholarIds.push(id);
20        }
21    } else {
22        uncachedSemanticScholarIds.push(id);
23    }
24});
25
26let showSemanticScholarCitationCount = () => {
27    // Update the DOM with the cached citation counts
28    semanticScholarIds.forEach(id => {
29        const cacheKey = `semanticScholarCitationCount:${id}`;
30        const cachedData = localStorage.getItem(cacheKey);
31        if (cachedData) {
32            const { citationCount } = JSON.parse(cachedData);
33            const elements = document.querySelectorAll(`[data-semantic-scholar-id="${id}"]`);
34            elements.forEach(element => {
35                element.innerHTML = `<a class="badge badge-pill badge-publication badge-info" href="https://www.semanticscholar.org/paper/${id}" target="_blank"><i class="ai ai-semantic-scholar"></i> ${parseInt(citationCount).toLocaleString()} citations</a>`;
36            });
37        }
38    });
39};
40
41if (uncachedSemanticScholarIds.length > 0) {
42    fetch('https://api.semanticscholar.org/graph/v1/paper/batch?fields=citationCount', {
43        method: 'POST',
44        headers: {
45            'Content-Type': 'application/json'
46        },
47        body: JSON.stringify({
48            ids: Array.from(semanticScholarIds)
49        })
50    }).then(response => {
51        return response.json();
52    }).then(data => {
53        data.forEach(paper => {
54            console.log(paper);
55            // Cache citation count data
56            const cacheKey = `semanticScholarCitationCount:${paper.paperId}`;
57            const cacheData = {
58                citationCount: paper.citationCount,
59                timestamp: Date.now()
60            };
61            localStorage.setItem(cacheKey, JSON.stringify(cacheData));
62        });
63    }).catch(error => {
64        console.error('Error fetching Semantic Scholar data:', error);
65    }).finally(showSemanticScholarCitationCount);
66} else {
67    showSemanticScholarCitationCount();
68}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.