User:Evad37/livenotifications.js

/******************************************************************************* Live notifications @overview Shows bubble notifications for new Echo notifications @author Evad37 @repo https://github.com/evad37/live-notifications @version 1.1.0 // !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:,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]); //