Jump to content
Wikipedia The Free Encyclopedia

User:Enterprisey/browser-notifs.js

From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Enterprisey/browser-notifs.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 //<nowiki>
 // !!!!!!!!!!!
 //
 // !!!!! you will need to click "notifs" under the More menu on a page to start receiving notifs
 //
 // !!!!!!!!!!!!!!!!
 $(function(){

 varexistingIds=[];
 vardate=newDate();
 varnow_utc=newDate(Date.UTC(date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate(),
 date.getUTCHours(),date.getUTCMinutes(),date.getUTCSeconds()));
 varapi=newmw.Api();

 // Copied from https://stackoverflow.com/a/3177838/1757964
 functiontimeSince(date){
 varseconds=Math.floor((newDate()-date)/1000);
 varinterval=Math.floor(seconds/31536000);
 if(interval>1){
 returninterval+" years";
 }
 interval=Math.floor(seconds/2592000);
 if(interval>1){
 returninterval+" months";
 }
 interval=Math.floor(seconds/86400);
 if(interval>1){
 returninterval+" days";
 }
 interval=Math.floor(seconds/3600);
 if(interval>1){
 returninterval+" hours";
 }
 interval=Math.floor(seconds/60);
 if(interval>1){
 returninterval+" minutes";
 }
 returnMath.floor(seconds)+" seconds";
 }

 functionwikilink(title){
 return"<a href='"+mw.util.getUrl(title)+"'>"+title+"</a>";
 }

 functionnotifToElement(notif){
 vartimestamp=timeSince(newDate(notif.timestamp.utciso8601))+" ago";
 varuserLink="<a href='"+mw.util.getUrl("User:"+notif.agent.name)+"' "
 +((notif.agent.name.length)>20?" style='font-size: 90%'":"")
 +">"+notif.agent.name+"</a>";
 varnewNotifDiv=$("<div class='notif'>");
 if(!notif.read)newNotifDiv.append("*");
 if(notif.type==="mention"&&notif.category==="mention"){
 newNotifDiv
 .append("Mentioned "+timestamp+" by "+userLink+" on ")
 .append(wikilink(notif.title.full))
 .append(" in ")
 .append("<a href='"+mw.util.getUrl("Special:Diff/"+notif.revid)+"'>this edit</a>");
 }
 elseif(notif.type==="emailuser"&&notif.category==="emailuser"){
 newNotifDiv
 .append("Emailed "+timestamp+" by "+userLink)
 }elseif(notif.type==="edit-thank"&&notif.category==="edit-thank"){
 newNotifDiv
 .append("Thanked "+timestamp+" by "+userLink+" for ")
 .append("<a href='"+mw.util.getUrl("Special:Diff/"+notif.revid)+"'>this edit</a>")
 .append(" to ")
 .append(wikilink(notif.title.full));
 }elseif(notif.type==="edit-user-talk"&&notif.category==="edit-user-talk"){
 newNotifDiv
 .append("Your user talk page changed "+timestamp+" by "+userLink)
 .append(" in ")
 .append("<a href='"+mw.util.getUrl("Special:Diff/"+notif.revid)+"'>this edit</a>");
 }elseif(notif.type==="mention-success"&&notif.category==="mention-success"){
 newNotifDiv
 .append("Successfully mentioned someone "+timestamp+" on ")
 .append(wikilink(notif.title.full))
 .append(" with ")
 .append("<a href='"+mw.util.getUrl("Special:Diff/"+notif.revid)+"'>this edit</a>");
 }elseif(notif.type==="mention-summary"&&notif.category==="mention"){
 newNotifDiv
 .append("Mentioned "+timestamp+" by "+userLink+" in the summary of ")
 .append("<a href='"+mw.util.getUrl("Special:Diff/"+notif.revid)+"'>this edit</a>");
 }elseif(notif.type==="login-fail-known"&&notif.category==="login-fail"){
 newNotifDiv
 .append("There have been 1 or more unsuccessful attempts to log in to your account since the last time you logged in. If it wasn't you, please make sure your account has a strong password.");
 }elseif(notif.type==="reverted"&&notif.category==="reverted"){
 newNotifDiv
 .append("Your edit to "+wikilink(notif.title.full)+" was ")
 .append("<a href='"+mw.util.getUrl("Special:Diff/"+notif.revid)+"'>reverted</a>")
 .append(" by "+userLink);
 }else{
 newNotifDiv.text(JSON.stringify(notif));
 }

 returnnewNotifDiv;
 }

 functionrefresh(){
 api.get({
 format:"json",
 action:"query",
 meta:"notifications",
 notunreadfirst:"true",// not is the notifications module, not the word "not"
 notlimit:50,
 formatversion:2,
 }).then(function(data){
 if(!data||!data.query||!data.query.notifications||
 !data.query.notifications.list){
 console.error("browser-notifs died ",data);
 }
 varnotifs=data.query.notifications.list;
 console.log(notifs,existingIds);
 notifs=notifs.filter(function(x){console.log(x,!x.read,(x.timestamp.utcunix>now_utc.getTime()/1000),x.timestamp.utcunix-now_utc.getTime()/1000,(existingIds.indexOf(x.id)<0));
 return!x.read&&(x.timestamp.utcunix>now_utc.getTime()/1000)&&(existingIds.indexOf(x.id)<0);});
 console.log(notifs);

 notifs.forEach(function(notif){

 console.log(notifs)
 Notification.requestPermission().then(function(permission){
 // If the user accepts, let's create a notification
 if(permission==="granted"){

 varnotification=newNotification(notifToElement(notif).get(0).textContent);
 if(notif.title&&notif.title.full){
 notification.onclick=function(){
 window.location.href=mw.util.getUrl(notif.title.full);
 };
 }else{
 notification.onclick=function(){
 window.location.href=mw.util.getUrl("Special:Notifications");
 };
 }
 }
 });

 existingIds.push(notif.id);
 });// end forEach
 });// end API callback
 }// end refresh()




 mw.loader.using(["mediawiki.util","mediawiki.api"]).then(function(){

 varlink=mw.util.addPortletLink("p-cactions","#","notifs","pt-notifs");
 if(link)link.addEventListener("click",function(){
 Notification.requestPermission();
 setInterval(function(){
 refresh();
 },2*60*1000);
 });
 });

 });
 //</nowiki>

AltStyle によって変換されたページ (->オリジナル) /