Jump to content

User:Evad37/livenotifications.js

From Wikipedia, the free encyclopedia
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.
/*******************************************************************************
 Live notifications
 @overview Shows bubble notifications for new Echo notifications
 @author Evad37
 @repo https://github.com/evad37/live-notifications
 @version 1.1.0
*******************************************************************************/
// <nowiki>
!function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){return o(e[i][1][r]||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(require,module,exports){"use strict";var obj,_App=(obj=require("./modules/App"))&&obj.__esModule?obj:{default:obj};Promise.all([mw.loader.using(["mediawiki.util","mediawiki.api","mediawiki.Title"]),$.ready]).then(function(){(0,_App.default)()})},{"./modules/App":2}],2:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var obj,_util=require("./util"),_spinner=(obj=require("./spinner"))&&obj.__esModule?obj:{default:obj};function _default(){var api=new mw.Api({ajax:{headers:{"Api-User-Agent":"livenotifications/1.1.0 ( https://en.wikipedia.org/wiki/User:Evad37/livenotifications )"}}}),config=mw.config.get(["wgServer"]),lastCheckedTimestamp=new Date;window.setInterval(function(){var loop=function(){if(document.hasFocus())return api.get({action:"query",format:"json",formatversion:"2",curtimestamp:1,meta:"notifications",notfilter:"!read",notformat:"model",notlimit:"20"}).then(function(response){(response&&response.query&&response.query.notifications&&response.query.notifications.list||[]).filter(function(notification){return lastCheckedTimestamp<new Date(notification.timestamp.utciso8601)}).forEach(function(notification){var iconFulllUrl="https:"+config.wgServer+notification["*"].iconUrl,readableDate=notification.timestamp.utciso8601.replace("T"," ").replace(/:\d\dZ/," (UTC)"),messageDiv=(0,_util.makeElement)("div",null,[(0,_util.makeElement)("span",{height:"30px",style:"float:right; margin:-0.2em -0.2em 0.2em 0.5em"},"X"),(0,_util.makeElement)("a",{href:notification["*"].links.primary.url,title:notification["*"].links.primary.label,target:"_blank",style:"display:block"},[(0,_util.makeElement)("img",{src:iconFulllUrl,height:"30px",style:"display:block; float:left; margin:0 0.5em 22em -0.5em; color:#666; font-size:88%"}),(0,_util.parseHtml)(notification["*"].header),(0,_util.makeElement)("span",{style:"display: block; color: #666; font-size:88%"},readableDate)].flatMap(function(x){return x}))]);mw.notify(messageDiv,{autoHide:!1})});lastCheckedTimestamp=new Date(response.curtimestamp)}).catch(function(error,details){return console.log("[livenotifications] error",{error:error,details:details})})}();loop&&(_spinner.default.start(),loop.then(function(){return _spinner.default.stop()}))},6e4)}exports.default=_default},{"./spinner":3,"./util":4}],3:[function(require,module,exports){"use strict";function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _default=new(function(){function Spinner(){!function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}(this,Spinner),this.badges=document.querySelectorAll(".mw-echo-notifications-badge"),this.minDuration=2500}var Constructor,protoProps,staticProps;return Constructor=Spinner,(protoProps=[{key:"start",value:function(){this.startTime=new Date,this.badges.forEach(function(el){return el.setAttribute("style","background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Ajax_loader_metal_512.gif/20px-Ajax_loader_metal_512.gif)")})}},{key:"stop",value:function(){var _this=this,timeUsed=new Date-this.startTime,remainingTime=Math.max(0,this.minDuration-timeUsed);window.setTimeout(function(){return _this.badges.forEach(function(el){return el.removeAttribute("style")})},remainingTime)}}])&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Spinner}());exports.default=_default},{}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parseHtml=exports.makeElement=void 0;exports.makeElement=function(tagName,attributes,contents){var el=document.createElement(tagName);if(attributes)for(var prop in attributes)el.setAttribute(prop,attributes[prop]);return contents&&(Array.isArray(contents)?contents:[contents]).forEach(function(content){var node=content&&content.nodeType?content:document.createTextNode(content);el.appendChild(node)}),el};exports.parseHtml=function(htmlString){var div=document.createElement("div");div.innerHTML=htmlString;var children=Array.from(div.childNodes);return 1===children.length?children[0]:children}},{}]},{},[1]);
// </nowiki>