Jandan+

煎蛋评论显示图链接

  1. // ==UserScript==
  2. // @name Jandan+
  3. // @namespace Jandan+@Byzod
  4. // @description 煎蛋评论显示图链接
  5. // @include /^https?:\/\/jandan\.net\//
  6. // @version 2016-11-24
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. function JandanPlus(){
  11. var self = this;
  12. var IMAGE_URL_REGEX = /(ftp|http)s?:\/\/[^\s\r\n]+\.(jpe?g|png|bmp|gif|ico|tga|tpic|tiff|svgz?|webp|jp2|j2c)/gi;
  13. self.RegisterCommentListMutation = function(){
  14. var commentLists = document.querySelectorAll(".commentlist");
  15. var observer = new MutationObserver(
  16. (e)=>{
  17. self.ParseImgURLTextInComment(e);
  18. }
  19. );
  20. var config = { childList: true, subtree: true };
  21. if(commentLists){
  22. for(var commentList of commentLists){
  23. observer.observe(commentList, config);
  24. }
  25. }
  26. };
  27. self.ParseImgURLTextInComment = function(e){
  28. for(var record of e){
  29. if(record.target.id == "ds-hot-posts" || (record.previousSibling && record.previousSibling.id == "ds-hot-posts")){
  30. // 评论
  31. var comments = record.addedNodes[1].querySelectorAll(".ds-comment-body");
  32. // console.log("jd+ comment-body: %o", comments);//debug
  33. for(var comment of comments){
  34. var commentText = comment.querySelector("p").innerHTML;
  35. var matches = commentText.match(IMAGE_URL_REGEX);
  36. if(matches){
  37. for(var i = 0; i < matches.length; i++){
  38. commentText = commentText.replace(matches[i], '<img src="' + matches[i] + '"></img>');
  39. // console.log("jd+ matches[%d]: %o;\n replace to: %o", i, matches[i], i, commentText);//debug
  40. }
  41. comment.querySelector("p").innerHTML = commentText;
  42. }
  43. }
  44. }
  45. }
  46. };
  47. }
  48. var oioi = new JandanPlus();
  49. oioi.RegisterCommentListMutation();