BS Notification

Benachrichtigung bei Erwähnung in der SB oder einer neuen PN und Userauswahl mit @ in der SB

当前为 2018-12-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name BS Notification
  3. // @namespace https://bs.to/
  4. // @version 4.5
  5. // @description Benachrichtigung bei Erwähnung in der SB oder einer neuen PN und Userauswahl mit @ in der SB
  6. // @author Asu_nyan
  7. // @match https://bs.to/*
  8. // @icon https://bs.to/favicon.ico
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/375096-bs-library/code/BS_Library.js
  11. // ==/UserScript==
  12. const BS = window.BS;
  13. let username = BS.Module.Get('.navigation strong').innerText;
  14. let alternatives = [`@${username}`];
  15. let userpattern = `(${ alternatives.join('|') })`;
  16. let regex = new RegExp(userpattern, "ig");
  17. let posts = BS.Module.Get('#sbPosts');
  18. posts.addEventListener("DOMNodeInserted", (a,b) => {
  19. if(a.target.localName == "dd" && regex.test(a.target.innerHTML))
  20. {
  21. occurrence();
  22. let datauser = a.target.previousSibling.dataset.user;
  23. let time = a.target.previousSibling.getElementsByTagName('time')[0].innerText;
  24. let title = (datauser != undefined) ? datauser : "Du wurdest erwähnt.";
  25. let options = {
  26. body: a.target.innerText + "\r\n" + time,
  27. icon: "https://bs.to/favicon.ico",
  28. };
  29. new Notification(title, options);
  30. }
  31. });
  32.  
  33. if (!Array.prototype.remI) {
  34. let ars = ["Array", "HTMLCollection"];
  35. for (let i = 0; i < ars.length; i++) {
  36. Object.defineProperty(eval(ars[i]).prototype, "remI", {
  37. enumerable: false,
  38. value: function (index) {
  39. for (let i = 0; i < this.length; i++) {
  40. if (i > index) {
  41. this[i - 1] = this[i];
  42. }
  43. }
  44. this.length--;
  45. }
  46. });
  47. Object.defineProperty(eval(ars[i]).prototype, "f", {
  48. enumerable: false,
  49. value: function findArray(f, equal = false, path = "", first = true) {
  50. let index = -1;
  51. for (let i = 0; i < this.length; i++) {
  52. if (equal) {
  53. if (f === eval("this[i]" + path)) {
  54. index = i;
  55. if (first) {
  56. return index;
  57. }
  58. }
  59. } else {
  60. if (f.toString().indexOf(eval("this[i]" + path)) > -1) {
  61. index = i;
  62. if (first) {
  63. return index;
  64. }
  65. }
  66. }
  67. }
  68. return index;
  69. }
  70. });
  71. }
  72. }
  73.  
  74. (function() {
  75. 'use strict';
  76. if (Notification.permission !== 'denied' || Notification.permission === 'default') {
  77. Notification.requestPermission((permission) => {
  78. if (permission === 'granted') {
  79. console.log(`Notification Permission: ${ permission }`);
  80. }
  81. });
  82. }
  83. setInterval(BS.PN.NotifyIfNew, 5000);
  84. occurrence();
  85. setTimeout(function(){
  86. let sB=BS.Module.Get('#sbMsg');
  87. let par=sB.parentElement;
  88. sB.oninput=function(a,b,c){
  89. let usercontainer=BS.Module.Get('#sbUser > ul').children;
  90. let onlineNames=[];
  91. for(let i=par.children.length-1;i>-1;i--){
  92. if(par.children[i].localName==="li12"){
  93. par.children[i].remove();
  94. }
  95. }
  96. for(let t=0;t<usercontainer.length;t++){
  97. if(sB.value.length>2){
  98. if(sB.value.indexOf("@")>-1 && usercontainer[t].children[0].textContent.toLowerCase().indexOf(sB.value.split("@")[1].toLowerCase())===0){
  99. onlineNames.push(usercontainer[t].children[0].textContent);
  100. }
  101. }
  102. }
  103. var active=BS.Module.Get('#sbPosts').children;
  104. for(let t=0;t<active.length;t+=2){
  105. if(onlineNames.f(active[t].children[0].textContent)>-1){
  106. onlineNames.remI(onlineNames.f(active[t].children[0].textContent,true));
  107. onlineNames.push(active[t].children[0].textContent);
  108. }else{
  109. if(sB.value.length>2&&sB.value.indexOf("@")>-1 && active[t].children[0].textContent.toLowerCase().indexOf(sB.value.split("@")[1].toLowerCase())===0){
  110. onlineNames.push(active[t].children[0].textContent);
  111. }
  112. }
  113.  
  114. }
  115. sB.onl=onlineNames;
  116. sB.index=0;
  117. if(sB.value.length>2&&onlineNames.length>0){
  118. let field=document.createElement("li12");
  119. field.style.width="200px";
  120. let height=onlineNames.length*20+20;
  121. let top=sB.offsetTop-(height)-5;
  122. field.style.position="absolute";
  123. field.style.top=top+"px";
  124. field.style.height=height+"px";
  125. field.style.left=sB.offsetLeft+"px";
  126. field.style.background="white";
  127. field.style.border="rgb(100, 160, 255) 1.5px solid";
  128. field.style.borderRadius= "4px";
  129. sB.fie=field;
  130. for(let j=0;j<onlineNames.length;j++){
  131. let username=document.createElement("el");
  132. username.innerText=onlineNames[j];
  133. username.style.position="absolute";
  134. username.style.top=j*20+8+"px";
  135. username.style.left=20+"px";
  136. username.style.width="160px";
  137. username.style.color="black";
  138. field.append(username);
  139. }
  140. par.append(field);
  141. }
  142. };
  143. sB.onkeydown=function(a,b,c){
  144. if(a.keyCode===38||a.keyCode===40){
  145. if(sB.fie.children[sB.index]){
  146. sB.fie.children[sB.index].style.backgroundColor="white";
  147. }
  148. function index(plus){
  149. if(plus){
  150. sB.index++;
  151. if(sB.index>sB.onl.length-1){
  152. sB.index=0;
  153. }
  154. }else{
  155. sB.index--;
  156. if(sB.index==-1){
  157. sB.index=sB.onl.length-1;
  158. }
  159. }
  160. }
  161. if(sB.index===undefined){
  162. sB.index=0;
  163. }
  164. if(a.keyCode===38){
  165. index(false);
  166. }else{
  167. index(true);
  168. }
  169. sB.fie.children[sB.index].style.backgroundColor="rgb(100, 160, 255)";
  170. sB.value=sB.value.replace(sB.value.split("@")[1].split(" ")[0],sB.onl[sB.index]+" ");
  171. }else if(a.keyCode===9){
  172. //tab
  173. if(sB.onl.length===1){
  174. sB.value=sB.value.replace(sB.value.split("@")[1].split(" ")[0],sB.onl[0]+" ");
  175. }else{
  176. sB.value=sB.value.replace(sB.value.split("@")[1].split(" ")[0],sB.onl[sB.onl.length-1]+" ");
  177. }
  178. for(let i=par.children.length-1;i>-1;i--){
  179. if(par.children[i].localName==="li12"){
  180. par.children[i].remove();
  181. }
  182. }
  183. setTimeout(function(sB){
  184. sB.focus();
  185. sB.selectionStart=sB.value.length;
  186. sB.selectionEnd=sB.value.length;
  187. },1,sB);
  188. }
  189. if(a.keyCode==13){
  190. return Shoutbox.checkEnter(a);
  191. }
  192. };
  193. },1000);
  194. })();
  195.  
  196. function occurrence(){
  197. let posts = document.getElementById('sbPosts');
  198. let postsContent = posts.children;
  199. for(let i = 0; i < postsContent.length; i++){
  200. if(postsContent[i].localName == "dd"){
  201. postsContent[i].innerHTML = postsContent[i].innerHTML.replace(regex, "<i style='color:yellow'>@"+username+"</i>");
  202. }
  203. }
  204. }