BS Notification

Benachrichtigung bei Erwähnung und Userauswahl mit @ in der SB

目前為 2018-06-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         BS Notification
// @namespace    https://bs.to/
// @version      3.0
// @description  Benachrichtigung bei Erwähnung und Userauswahl mit @ in der SB
// @author       Asu
// @match        https://bs.to
// @match        https://bs.to/home
// @icon         https://bs.to/favicon.ico
// @grant        none
// ==/UserScript==


var customuserarray=[];
var username=document.getElementById("navigation").children[0].childNodes[1].innerText;
var userpattern = "@" + username;
var regex = new RegExp(userpattern, "ig");

function x (){
    let posts = document.getElementById('sbPosts');
    let postsContent = posts.children;
    for(let i = 0; i < postsContent.length; i++){
        if(postsContent[i].localName == "dd"){
            postsContent[i].innerHTML = postsContent[i].innerHTML.replace(regex, "<i style='color:yellow'>@"+username+"</i>");
        }
    }
}


try{
	username=document.getElementById("navigation").children[0].childNodes[1].innerText;
}catch(err){
	console.err(err);
}
if (!Array.prototype.remI) {
	var ars = ["Array", "HTMLCollection"];
	for (var i = 0; i < ars.length; i++) {
		Object.defineProperty(eval(ars[i]).prototype, "remI", {
			enumerable: false,
			value: function (index) {
				for (var i = 0; i < this.length; i++) {
					if (i > index) {
						this[i - 1] = this[i];
					}
				}
				this.length--;
			}
		});
		Object.defineProperty(eval(ars[i]).prototype, "f", {
			enumerable: false,
			value: function findArray(f, equal = false, path = "", first = true) {
				var index = -1;
				for (var i = 0; i < this.length; i++) {
					if (equal) {
						if (f === eval("this[i]" + path)) {
							index = i;
							if (first) {
								return index;
							}
						}
					} else {
						if (f.toString().indexOf(eval("this[i]" + path)) > -1) {
							index = i;
							if (first) {
								return index;
							}
						}
					}
				}
				return index;
			}
		});
	}
}


(function() {
    'use strict';
    let posts = document.getElementById('sbPosts');
    posts.addEventListener("DOMNodeInserted", (a,b) => {
        if(a.target.localName == "dd" && regex.test(a.target.innerHTML))
        {
            x();
            let datauser = a.target.previousSibling.dataset.user;
            let title = (datauser != undefined) ? datauser : "Neue Benachrichtigung";
            let options = {
                body: a.target.innerText,
                icon: "https://bs.to/favicon.ico",
            };
            // Ding: https://www.myinstants.com/media/sounds/ding_2.mp3
            // Forum Notification: https://board.bs.to/applications/core/interface/sounds/notification.mp3
            // Surprise Motherfucker: https://dl.dropboxusercontent.com/content_link/0sJVLvb4JIFKg5IavxBUmVAkHFUyZ91AfsNs6PXnNSrlaQmpVQpLlWrrfzUC7pQD/file?dl=0&duc_id=dropbox_duc_id
            var sound = new Audio("https://board.bs.to/applications/core/interface/sounds/notification.mp3");
            sound.play();
            let n = new Notification(title, options);
        }
    });
    // Your code here...
    //setInterval(x, 1000);
    x();

    //übernommener code:

	setTimeout(function(){
		try{
			username=document.getElementById("navigation").children[0].childNodes[1].innerText;
		}catch(err){}
		var sB=$(sbMsg)[0];
		var par=sB.parentElement;
		sB.oninput=function(a,b,c){
			var usercontainer=$(sbUserCont)[0].children;
			var onlineNames=[];
			for(var i=par.children.length-1;i>-1;i--){
				if(par.children[i].localName==="li12"){
					par.children[i].remove();
				}
			}
			for(var t=0;t<usercontainer.length;t++){
				if(sB.value.length>2){
					if(sB.value.indexOf("@")>-1 && usercontainer[t].children[0].textContent.toLowerCase().indexOf(sB.value.split("@")[1].toLowerCase())===0){
						onlineNames.push(usercontainer[t].children[0].textContent);
					}
				}
			}
			var active=$(sbPosts)[0].children;
			for(var t=0;t<active.length;t+=2){
				if(onlineNames.f(active[t].children[0].textContent)>-1){
					onlineNames.remI(onlineNames.f(active[t].children[0].textContent,true));
					onlineNames.push(active[t].children[0].textContent);
				}else{
					if(sB.value.length>2&&sB.value.indexOf("@")>-1 && active[t].children[0].textContent.toLowerCase().indexOf(sB.value.split("@")[1].toLowerCase())===0){
						onlineNames.push(active[t].children[0].textContent);
					}
				}

			}
			sB.onl=onlineNames;
			sB.index=0;
			if(sB.value.length>2&&onlineNames.length>0){
				var field=document.createElement("li12");
				field.style.width="200px";
				var height=onlineNames.length*20+20;
				var top=sB.offsetTop-(height)-5;
				field.style.position="absolute";
				field.style.top=top+"px";
				field.style.height=height+"px";
				field.style.left=sB.offsetLeft+"px";
				field.style.background="white";
				field.style.border="rgb(100, 160, 255) 1.5px solid";
				field.style.borderRadius= "4px";
				sB.fie=field;
				for(var j=0;j<onlineNames.length;j++){
					var username=document.createElement("el");
					username.innerText=onlineNames[j];
					username.style.position="absolute";
					username.style.top=j*20+8+"px";
					username.style.left=20+"px";
					username.style.width="160px";
                    username.style.color="black";
					field.append(username);
				}
				par.append(field);
			}
		};
		sB.onkeydown=function(a,b,c){
			if(a.keyCode===38||a.keyCode===40){
				if(sB.fie.children[sB.index]){
					sB.fie.children[sB.index].style.backgroundColor="white";
				}
				function index(plus){
					if(plus){
						sB.index++;
						if(sB.index>sB.onl.length-1){
							sB.index=0;
						}
					}else{
						sB.index--;
						if(sB.index==-1){
							sB.index=sB.onl.length-1;
						}
					}
				}
				if(sB.index===undefined){
					sB.index=0;
				}
				if(a.keyCode===38){
					index(false);
				}else{
					index(true);
				}
				sB.fie.children[sB.index].style.backgroundColor="rgb(100, 160, 255)";
				sB.value=sB.value.replace(sB.value.split("@")[1].split(" ")[0],sB.onl[sB.index]+" ");
			}else if(a.keyCode===9){
				//tab
				if(sB.onl.length===1){
					sB.value=sB.value.replace(sB.value.split("@")[1].split(" ")[0],sB.onl[0]+" ");
				}else{
					sB.value=sB.value.replace(sB.value.split("@")[1].split(" ")[0],sB.onl[sB.onl.length-1]+" ");
				}
				for(var i=par.children.length-1;i>-1;i--){
					if(par.children[i].localName==="li12"){
						par.children[i].remove();
					}
				}
				setTimeout(function(sB){
					sB.focus();
					sB.selectionStart=sB.value.length;
					sB.selectionEnd=sB.value.length;
				},1,sB);
			}
			if(a.keyCode==13){
				return Shoutbox.checkEnter(a);
			}
		};
	},1000);

})();