v2新帖挂件

v2右侧挂件,展示最近发表的帖子

  1. // ==UserScript==
  2. // @name v2新帖挂件
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.11
  5. // @description v2右侧挂件,展示最近发表的帖子
  6. // @author HeyWeGo
  7. // @match *://*.v2ex.com/*
  8. // @icon https://www.v2ex.com/static/favicon.ico
  9. // @grant GM_xmlhttpRequest
  10. // @connect *
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15.  
  16. function htmlToElement(html) {
  17. var template = document.createElement('template');
  18. html = html.trim();
  19. template.innerHTML = html;
  20. return template.content.firstChild;
  21. }
  22.  
  23.  
  24. function htmlToElements(html) {
  25. var template = document.createElement('template');
  26. template.innerHTML = html;
  27. return template.content.childNodes;
  28. }
  29.  
  30. let div_wrap = htmlToElements('<div class="box" id="sb_v2" style="position:relative;max-height:500px;overflow-x:hidden;overflow-y:scroll"><div id="new_head" class="cell" style="background: var(--box-background-color);top: 0;position: sticky;z-index: 999;margin-bottom:14px;"><span class="fade" >最近发布主题</span><span><a class="fade refresh_new" style="margin-left:9px;color:var(--link-color);cursor:pointer">刷新</a></span></div></div>')
  31. let list_wrap = div_wrap[0];
  32. let refresh_new_btn = list_wrap.getElementsByClassName("refresh_new")[0];
  33. // let auto_refresh = list_wrap.getElementsByClassName("autorefresh")[0];
  34. var loop;
  35.  
  36. let widget_target = document.getElementById("Rightbar").getElementsByClassName("box")[0];
  37. widget_target.append(list_wrap)
  38.  
  39. let from_refresh = false;
  40.  
  41. refresh_new_btn.addEventListener("click",function(){
  42. from_refresh = true;
  43. console.log("刷新列表")
  44. let el = document.createElement("div")
  45. el.id = "loading_frame"
  46. el.innerHTML = "loading";
  47. el.style="height:3000px;position: absolute;background: #ffffffd6;width: 100%;vertical-align: middle;z-index: 999;top:47px;padding-top:200px;"
  48. list_wrap.appendChild(el)
  49.  
  50. new_widget();
  51.  
  52. })
  53.  
  54.  
  55. function frag_generate(in_titles,in_links){
  56. let frag = document.createDocumentFragment();
  57. let element = document.createElement('div');
  58. element.id = 'new_list'
  59. for (let index = 1; index < in_titles.length; index++) {
  60. var nodeText;
  61. var puretitle = in_titles[index].textContent.replace(/^\[.*?\]/,'');
  62.  
  63. nodeText = in_titles[index].textContent.match(/^\[.*?\]/)[0];
  64.  
  65. let tr_head = '<div class="cell" style="position:relative"><table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody><tr><td class="topic_type" width="24" valign="middle" align="center" style="color: var(--color-fade);padding: 0 13px;white-space: nowrap;min-width: 66px;position: absolute;transform: scale(0.8);top: -12px;right: 4px;background: var(--box-background-color);">'
  66. let tr_head_wrap = tr_head + nodeText + '</td><td width="10"></td><td width="auto" valign="middle"><span class="item_rss_new_topic_title"><a style="text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2;-webkit-box-orient: vertical; " target="_blank" href="';
  67. let row_string = tr_head_wrap + in_links[index+1].getAttribute('href') + '">' + puretitle +'</a></span></td></tr></tbody></table></div>'
  68. element.appendChild(htmlToElements(row_string)[0]);
  69. }
  70. return frag.appendChild(element);
  71. }
  72.  
  73.  
  74.  
  75. function new_widget(){
  76.  
  77. GM_xmlhttpRequest({
  78. url: "https://www.v2ex.com/index.xml",
  79. method :"GET",
  80. onload:function(xhr){
  81. console.log(xhr.responseText);
  82. var data = xhr.responseText
  83. let xmlDoc = new DOMParser().parseFromString(data, "text/xml");
  84. let titles=xmlDoc.getElementsByTagName('title')
  85. let links=xmlDoc.getElementsByTagName('link')
  86. console.log("请求成功")
  87.  
  88. let el = document.getElementById("new_list")
  89. if(!!el === true){
  90. el.remove();
  91. }
  92. list_wrap.append(frag_generate(titles,links));
  93.  
  94. if(from_refresh === true){
  95. document.getElementById("loading_frame").remove();
  96. from_refresh = false;
  97. }
  98. }
  99.  
  100. })
  101.  
  102. }
  103.  
  104. new_widget()
  105.  
  106.  
  107. })();

QingJ © 2025

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