From 05b6e2f45d3fd5a349c341e8f43f68f18870fa1d Mon Sep 17 00:00:00 2001 From: SPC Date: Tue, 13 Feb 2024 16:03:20 +0800 Subject: [PATCH] feat: Add semantic API for searching venue Signed-off-by: SPC --- js/semanticscholar.js | 75 +++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/js/semanticscholar.js b/js/semanticscholar.js index 6e82446..be6bd10 100644 --- a/js/semanticscholar.js +++ b/js/semanticscholar.js @@ -8,12 +8,16 @@ const semanticscholar = {}; semanticscholar.rankSpanList = []; +let count = 0; +let maxCount = 5; + semanticscholar.run = function () { let url = window.location.pathname; - setInterval(function () { - $(window).bind("popstate", function () { - semanticscholar.appendRanks(); - }); + let intervalId = setInterval(function () { + count++; + if (count === maxCount) { + clearInterval(intervalId); + } semanticscholar.appendRanks(); }, 700); }; @@ -24,31 +28,54 @@ semanticscholar.appendRanks = function () { let element = $(this); let source = element[0].innerText; if (source.length != 0 && !element.next().hasClass("ccf-rank")) { - for (let getRankSpan of semanticscholar.rankSpanList) { - if (source.includes('(')) { - source = source.substring(source.indexOf('(') + 1, source.indexOf(')')); - } - if (source.includes('\'')) { - source = source.substring(0, source.indexOf('\'')).trim(); - } - element.after(getRankSpan(source, 'abbr')); + let paperNode = element.parents('.cl-paper-row').first(); + // - a Semantic Scholar ID + let paperId = paperNode.attr('data-paper-id'); + if (paperId == undefined) { + // In paper detail page, i.e. https://www.semanticscholar.org/paper/... + paperNode = element.parents("[data-test-id='paper-detail-page-header']").first(); + paperNode = paperNode.find("[data-test-id='corpus-id']").first(); + // CorpusId: - a Semantic Scholar numerical ID + paperId = "CorpusId:" + paperNode.text().match(/\d+/)[0]; } + let appendRankBar = semanticscholar.appendRankBar; + semanticscholar.fetchData(paperId, element, appendRankBar); } }); }; -semanticscholar.appendRank = function (selector) { - let element = $(selector); - let headline = window.location.pathname; - if (headline.length != 0) { - for (let getRankSpan of dblp.rankSpanList) { - let urls = headline.substring( - headline.indexOf("/db/") + 3, - headline.lastIndexOf("/") - ); - url = ccf.rankDb[urls]; - element.after(getRankSpan(url, "url")); +let semanticAPI = `https://api.semanticscholar.org/graph/v1/paper/{paper_id}?fields=venue`; + +semanticscholar.fetchData = function (paperId, element, callback) { + let query_url = semanticAPI.replace('{paper_id}', paperId); + let cached = apiCache.getItem(query_url); + if(cached) { + venue = cached.venue; + callback(element, venue); + return; + } + // none cached + $.get(query_url) + .done(function (data) { + apiCache.setItem(query_url, data); + venue = data.venue; + callback(element, venue); + }) + .fail(function () { + console.log("Request failed."); + }); +} + +semanticscholar.appendRankBar = function (selector, source) { + if (selector.next().hasClass("ccf-rank")) return; + + for (let getRankSpan of semanticscholar.rankSpanList) { + if (source.includes('(')) { + source = source.substring(source.indexOf('(') + 1, source.indexOf(')')); + } + if (source.includes('\'')) { + source = source.substring(0, source.indexOf('\'')).trim(); } + selector.after(getRankSpan(source, 'abbr')); } }; - \ No newline at end of file