IF Preview

Add IF to Google Scholar Search Result!

  1. // ==UserScript==
  2. // @name IF Preview
  3. // @namespace https://github.com/Zwysun
  4. // @version 1.0
  5. // @description Add IF to Google Scholar Search Result!
  6. // @author Zwysun
  7. // @match https://scholar.google.com/*
  8. // @match https://scholar.google.com.hk/*
  9. // @icon https://www.google.com/s2/favicons?domain=tampermonkey.net
  10. // @connect scholar.google.com
  11. // @connect api.scholarscope.cn
  12. // @connect self
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @run-at document_idle
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. console.log("hello");
  23. // GM_setValue('publication_if_info', {});
  24. // GM_setValue('paper_id_name_info', {});
  25. // return;
  26.  
  27. var impact_factor_dict = GM_getValue('publication_if_info', {}); // key, publication name
  28. var publication_name_dict = GM_getValue('paper_id_name_info', {}); // key, paper id
  29. var uid_list = parse_paper_id();
  30. var indication_count = 0;
  31. var max_timeout = 5000;
  32.  
  33. // 页面不匹配时直接退出
  34. if (uid_list.length <= 0) {
  35. console.log('Cannot Work in This Page!')
  36. return;
  37. }
  38.  
  39. // console.log(impact_factor_dict);
  40. // console.log(publication_name_dict);
  41.  
  42. // get_IF_with_name('22', 'IEEE');
  43. for (var i = 0; i < uid_list.length; i++) {
  44. var tmp_uid = uid_list[i];
  45. // 没保存时重新获取
  46. if (!publication_name_dict.hasOwnProperty(tmp_uid)) {
  47. get_full_publication_name_google(tmp_uid);
  48. } else {
  49. var publication_name = publication_name_dict[tmp_uid];
  50. if (!impact_factor_dict.hasOwnProperty(publication_name)) {
  51. // console.log('===========');
  52. // console.log(publication_name);
  53. get_IF_with_name(publication_name);
  54. } else {
  55. indication_count++;
  56. }
  57.  
  58. }
  59. }
  60. // console.log('----------------');
  61. // console.log(indication_count);
  62. var loop_count = 0;
  63. main_loop();
  64.  
  65. // 轮询信息是否获取结束
  66. function main_loop() {
  67. console.log('loop')
  68. console.log(loop_count);
  69. if (loop_count < max_timeout/100)
  70. {
  71. setTimeout(function () {
  72. console.log('开始查询');
  73. console.log(indication_count);
  74. loop_count++;
  75. if (indication_count < uid_list.length) {
  76. main_loop();
  77. return;
  78. } else {
  79. preview_and_save();
  80. return;
  81. }
  82. }, 100);
  83. } else {
  84. preview_and_save();
  85. return;
  86. }
  87. }
  88.  
  89.  
  90. //--------函数----------//
  91.  
  92. function preview_and_save() {
  93. for (var i = 0; i < uid_list.length; i++) {
  94. var tmp_uid = uid_list[i];
  95. add_if_to_page(tmp_uid);
  96. }
  97. GM_setValue('publication_if_info', impact_factor_dict);
  98. GM_setValue('paper_id_name_info', publication_name_dict);
  99. }
  100.  
  101. // 获取文献的ID列表
  102. function parse_paper_id() {
  103. var all_uid_element_list = document.getElementsByClassName('gs_r gs_or gs_scl');
  104. var uid_array = [];
  105. for (var i=0;i<all_uid_element_list.length;i++) {
  106. var data_uid = all_uid_element_list[i].getAttribute('data-cid');
  107. uid_array.push(data_uid);
  108. // console.log(data_uid);
  109. }
  110. return uid_array;
  111. }
  112.  
  113. // 添加信息到页面
  114. function add_if_to_page(paper_id) {
  115. try {
  116. var publication_name = publication_name_dict[paper_id];
  117. // 无查询结果时直接跳过
  118. var publication_info = impact_factor_dict[publication_name];
  119. if (publication_info == 0) {
  120. return;
  121. }
  122.  
  123. // 根据Paper ID 定位并插入显示
  124. var uid_object_list = document.getElementsByClassName('gs_r gs_or gs_scl');
  125. var select_index = -1;
  126. for(var i=0;i<uid_object_list.length;i++){
  127. if(uid_object_list[i].getAttribute('data-cid')==paper_id){
  128. // console.log(uid_object_list[i]);
  129. select_index = i;
  130. }
  131. }
  132. if (select_index==-1){
  133. console.log('No This Element!');
  134. return false;
  135. }
  136.  
  137. var pre_to_add_element = uid_object_list[select_index];
  138. var target_element = pre_to_add_element.getElementsByClassName('gs_ggs gs_fl');
  139. var tmp_html = '';
  140. var add_html = '';
  141.  
  142. // 判断是否已经存在IF注释
  143. if (pre_to_add_element.getElementsByClassName('if_preview').length != 0) {
  144. return;
  145. }
  146.  
  147. // 添加方式的不同
  148. if (target_element.length>0) {
  149. tmp_html = target_element[0].getInnerHTML();
  150. add_html = '<div class="if_preview" style="font-size:13px">'+publication_name_dict[paper_id]+'<br><strong>IF: '+publication_info.IF+'</strong><br><a href="'+publication_info.URL+'" target="_blank">View Detail</a></div>';
  151. target_element[0].innerHTML = tmp_html + add_html;
  152. return true;
  153. }
  154. else if (target_element.length==0) {
  155. tmp_html = pre_to_add_element.getInnerHTML();
  156. add_html = '<div class="gs_ggs gs_fl"><div class="if_preview" style="font-size:13px">'+publication_name_dict[paper_id]+'<br><strong>IF: '+publication_info.IF+'</strong><br><a href="'+publication_info.URL+'" target="_blank">查看详情</a></div></div>';
  157. pre_to_add_element.innerHTML = add_html + tmp_html;
  158. return true;
  159. }
  160. } catch (error) {
  161. console.log(error);
  162. return false;
  163. }
  164. }
  165.  
  166. // 根据ID获取完整的出版商名
  167. function get_full_publication_name_google(info_id) {
  168. // return info_id;
  169. try {
  170. // 请求获取完整期刊名称
  171. var current_user_agent = navigator.userAgent;
  172. var refer_url = window.location.href;
  173. var current_host = window.location.host;
  174. GM_xmlhttpRequest({
  175. method: "GET",
  176. url: "https://"+ current_host + "/scholar?q=info:" + info_id + ":scholar.google.com/&output=cite&scirp=0&hl=zh-CN",
  177. cookie: document.cookie,
  178. headers: {
  179. "accept-encoding": "gzip, deflate, br",
  180. "accept-language": "zh-CN,zh;q=0.9",
  181. "user-agent": current_user_agent,
  182. "referer": refer_url,
  183. "sec-ch-ua-mobile": "?0",
  184. "x-requested-with": "XHR",
  185. "sec-fetch-site": "same-site"
  186. },
  187. onload: function(response) {
  188. // 解析返回数据 获取完整的期刊名称
  189. // console.log(current_user_agent);
  190. // console.log(refer_url);
  191. var el = document.createElement('html');
  192. el.innerHTML = response.responseText;
  193.  
  194. var publication_name = el.getElementsByTagName("i")[0].childNodes[0].nodeValue;
  195. publication_name_dict[info_id] = publication_name;
  196.  
  197. // 获取IF
  198. if (!impact_factor_dict.hasOwnProperty(publication_name)) {
  199. get_IF_with_name(publication_name);
  200. }
  201. // else {
  202. // get_IF_with_name(info_id, publication_name);
  203. // }
  204. // console.log(el.getElementsByTagName("i")[0].childNodes[0].nodeValue);
  205. },
  206. onabort: reportAJAX_Error,
  207. onerror: reportAJAX_Error,
  208. ontimeout: reportAJAX_Error
  209. });
  210. } catch (error) {
  211. console.log(error);
  212. return false;
  213. }
  214. }
  215.  
  216. // 根据出版商名获取影响因子IF
  217. function get_IF_with_name(publication_name) {
  218. try {
  219. var current_user_agent = navigator.userAgent;
  220. GM_xmlhttpRequest({
  221. method: "POST",
  222. url: "https://api.scholarscope.cn/getsinglesearch.php",
  223. headers: {
  224. "accept-encoding": "gzip, deflate, br",
  225. "accept-language": "zh-CN,zh;q=0.9",
  226. "user-agent": current_user_agent,
  227. "content-type": "application/x-www-form-urlencoded",
  228. "origin": "https://www.scholarscope.cn",
  229. "referer": "https://www.scholarscope.cn/",
  230. "sec-fetch-site": "same-site"
  231. },
  232. data: "jrnl="+publication_name,
  233. nocache: true,
  234. onload: function(response) {
  235. // 解析返回结果中匹配的期刊名称以及影响因子
  236. // console.log(current_user_agent);
  237. // console.log(refer_url);
  238.  
  239. var el = document.createElement('html');
  240. el.innerHTML = response.responseText;
  241.  
  242. // console.log(publication_name);
  243. // console.log(response.responseText);
  244. var tr_element_list = el.getElementsByTagName('table')[0].getElementsByTagName('tr');
  245. var td_element_list = tr_element_list[1].getElementsByTagName('td'); // 初始为第一行查询数据
  246.  
  247. var find_right = false;
  248. var need_check = false;
  249.  
  250. // 元素过少时直接跳过
  251. if (td_element_list.length < 4) {
  252. impact_factor_dict[publication_name] = 0;
  253. console.log('No Result!');
  254. // 指示计数+1
  255. indication_count++;
  256. return;
  257. }
  258.  
  259. // 循环查找tr中最匹配的,否则使用第一个
  260. for (var i=1; i<tr_element_list.length; i++) {
  261. var tmp_new_publication_name = tr_element_list[i].getElementsByTagName('td')[0].childNodes[0].nodeValue;
  262.  
  263. if (tmp_new_publication_name.replace(/[^0-9A-Za-z]+/g,"").toLowerCase() == publication_name.replace(/[^0-9A-Za-z]+/g,"").toLowerCase())
  264. {
  265. td_element_list = tr_element_list[i].getElementsByTagName('td');
  266. need_check = false;
  267. find_right = true;
  268. break;
  269. }
  270. }
  271.  
  272. // 没有找到匹配的 取第一个
  273. if (!find_right) {
  274. td_element_list = tr_element_list[1].getElementsByTagName('td');
  275. need_check = true;
  276. }
  277.  
  278. var impact_factor = td_element_list[2].childNodes[0].nodeValue;
  279. var detail_url = encodeURI(td_element_list[3].getElementsByTagName('a')[0].getAttribute('href'));
  280.  
  281. // 生成info字典并存储
  282. var publication_info = {'IF':impact_factor, 'URL':detail_url, "CHECK":need_check};
  283. impact_factor_dict[publication_name] = publication_info;
  284. // console.log("Show IF");
  285. // console.log(impact_factor);
  286. // GM_setValue('publication_info', impact_factor_dict);
  287.  
  288. // add_if_to_page(info_id, publication_name);
  289. // 指示计数+1
  290. indication_count++;
  291. },
  292. onabort: reportAJAX_Error,
  293. onerror: reportAJAX_Error,
  294. ontimeout: reportAJAX_Error
  295. });
  296. } catch (error) {
  297. console.log(error);
  298. return false;
  299. }
  300. }
  301.  
  302. function reportAJAX_Error (respObject) {
  303. console.log('Error ' + respObject.status + '! "' + respObject.statusText + '"');
  304. }
  305. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址