Difference between revisions of "User:Wookipan/common.js"

From Team Fortress Wiki
Jump to: navigation, search
m
(🏗️🚧)
Line 1: Line 1:
 +
    function Notification(page, inGroup, autoHide) {
 +
        this.init = function() {
 +
            if (page !== undefined) {
 +
                var isAction = 0;
 +
                this.prefix = 'User:Wookipan/';
 +
                this.page = this.prefix + page;
 +
                this.inGroup = inGroup.split(', ');
 +
 +
                if (autoHide !== undefined) {
 +
                    this.options = { autoHide: autoHide };
 +
                }
 +
 +
                this.setAction = function(action, regex) {
 +
                    isAction = 1;
 +
                    if (action !== undefined && window.location.href.indexOf(action) > -1) {
 +
                        if (regex !== undefined) {
 +
                            var selectors = {
 +
                                input: document.getElementById('wpSummary'),
 +
                                textarea: document.getElementById('wpTextbox1')
 +
                            };
 +
 +
                            var regExp = new RegExp('(?:.+' + regex + '.+)');
 +
                            var value = selectors.input.value;
 +
                            var text = selectors.textarea.textContent;
 +
                            var isMatch = Boolean(value.match(regExp) || text.match(regExp));
 +
 +
                            if (isMatch) {
 +
                                this.userGroup(this.inGroup);
 +
                            }
 +
                        } else {
 +
                            this.userGroup(this.inGroup);
 +
                        }
 +
                    }
 +
                }
 +
 +
                setTimeout(function() {
 +
                    if (!isAction) {
 +
                        this.userGroup(this.inGroup);
 +
                    }
 +
                }.bind(this), 1000);
 +
            }
 +
        }
 +
 +
        this.init();
 +
    }
 +
 +
    Notification.prototype = Object.assign({}, Notification.prototype, {
 +
        query: function(page, options) {
 +
            if (page) {
 +
                var params = {
 +
                    action: 'parse',
 +
                    page: page,
 +
                    prop: 'text',
 +
                    format: 'json'
 +
                };
 +
 +
                var api = new mw.Api();
 +
                api.get(params).done(function(data) {
 +
                    var content = data.parse.text['*'];
 +
 +
                    var getMarkup = function(html) {
 +
                        var parser = new DOMParser();
 +
                        var markup = parser.parseFromString(html, 'text/html');
 +
                        var table = markup.body.querySelector('table');
 +
                        var links = table.querySelectorAll('a');
 +
 +
                        if (links.length) {
 +
                            links.forEach(function(link) {
 +
                                // Open links in a new tab to avoid forcing the user to save or leave their edit.
 +
                                link.setAttribute('target', '_blank');
 +
                            });
 +
                        }
 +
 +
                        if (!table) {
 +
                            return;
 +
                        } else {
 +
                            return table;
 +
                        }
 +
                    };
 +
 +
                    mw.notify(getMarkup(content), options);
 +
                });
 +
            }
 +
        },
 +
 +
        userGroup: function(group) {
 +
            this.groups = mw.config.get('wgUserGroups');
 +
            var inGroup = group.every(function (e) {
 +
                return this.groups.indexOf(e) > -1;
 +
            }.bind(this));
 +
 +
            return this.validateGroup(group, inGroup);
 +
        },
 +
 +
        validateGroup: function(group, inGroup) {
 +
            var isNew = Boolean(!this.groups.includes('autoconfirmed') && this.groups.includes('user'));
 +
 +
            if (inGroup || group[0] === 'all') {
 +
                this.query(this.page, this.options);
 +
            }
 +
 +
            if (isNew && group[0] === 'new') {
 +
                this.query(this.page, this.options);
 +
            }
 +
 +
            return isNew;
 +
        }
 +
    });
 +
 +
    return Notification;
 +
})();
 +
 +
var notification = {
 +
init: function() {
 +
new system.Notification('NotifTest', 'all', false); // global notice
 +
new system.Notification('NotifTest', 'sysop', false).setAction('section', 'Trivia'); // section-based notice, targeted user group
 +
},
 +
};
 +
$(notification.init);
 +
 
// Start custom username highlighting -----
 
// Start custom username highlighting -----
 
/*
 
/*

Revision as of 22:04, 15 August 2023

    function Notification(page, inGroup, autoHide) {
        this.init = function() {
            if (page !== undefined) {
                var isAction = 0;
                this.prefix = 'User:Wookipan/';
                this.page = this.prefix + page;
                this.inGroup = inGroup.split(', ');

                if (autoHide !== undefined) {
                    this.options = { autoHide: autoHide };
                }

                this.setAction = function(action, regex) {
                    isAction = 1;
                    if (action !== undefined && window.location.href.indexOf(action) > -1) {
                        if (regex !== undefined) {
                            var selectors = {
                                input: document.getElementById('wpSummary'),
                                textarea: document.getElementById('wpTextbox1')
                            };

                            var regExp = new RegExp('(?:.+' + regex + '.+)');
                            var value = selectors.input.value;
                            var text = selectors.textarea.textContent;
                            var isMatch = Boolean(value.match(regExp) || text.match(regExp));

                            if (isMatch) {
                                this.userGroup(this.inGroup);
                            }
                        } else {
                            this.userGroup(this.inGroup);
                        }
                    }
                }

                setTimeout(function() {
                    if (!isAction) {
                        this.userGroup(this.inGroup);
                    }
                }.bind(this), 1000);
            }
        }

        this.init();
    }

    Notification.prototype = Object.assign({}, Notification.prototype, {
        query: function(page, options) {
            if (page) {
                var params = {
                    action: 'parse',
                    page: page,
                    prop: 'text',
                    format: 'json'
                };

                var api = new mw.Api();
                api.get(params).done(function(data) {
                    var content = data.parse.text['*'];

                    var getMarkup = function(html) {
                        var parser = new DOMParser();
                        var markup = parser.parseFromString(html, 'text/html');
                        var table = markup.body.querySelector('table');
                        var links = table.querySelectorAll('a');

                        if (links.length) {
                            links.forEach(function(link) {
                                // Open links in a new tab to avoid forcing the user to save or leave their edit.
                                link.setAttribute('target', '_blank');
                            });
                        }

                        if (!table) {
                            return;
                        } else {
                            return table;
                        }
                    };

                    mw.notify(getMarkup(content), options);
                });
            }
        },

        userGroup: function(group) {
            this.groups = mw.config.get('wgUserGroups');
            var inGroup = group.every(function (e) {
                return this.groups.indexOf(e) > -1;
            }.bind(this));

            return this.validateGroup(group, inGroup);
        },

        validateGroup: function(group, inGroup) {
            var isNew = Boolean(!this.groups.includes('autoconfirmed') && this.groups.includes('user'));

            if (inGroup || group[0] === 'all') {
                this.query(this.page, this.options);
            }

            if (isNew && group[0] === 'new') {
                this.query(this.page, this.options);
            }

            return isNew;
        }
    });

    return Notification;
})();

var notification = {
	init: function() {
		new system.Notification('NotifTest', 'all', false); // global notice
		new system.Notification('NotifTest', 'sysop', false).setAction('section', 'Trivia'); // section-based notice, targeted user group
	},
};
$(notification.init);

// Start custom username highlighting -----
/*
var uGroupHighlight = {
  init: function() {
    if ($('.mw-userlink')[0]) {
      var params = {
        action: 'query',
        list: 'allusers',
        augroup: ['sysop', 'moderator', 'bot'],
        auprop: 'groups',
        aulimit: 100,
        format: 'json'
      };

      var api = new mw.Api();

      api.get(params).done(function(data) {
        var uGroups = data.query.allusers, user;
        for (user in uGroups) {
          var group = uGroups[user].groups;
          var name = uGroups[user].name;

          $('bdi').each(function() {
            if ($(this).text().match('\\b' + name + '\\b')) {
              $(this).closest('.mw-userlink').addClass(group.includes('bot') ? 'bot' : 'staff');
            }
          });
        }
      });
    }
  },
};
$(uGroupHighlight.init);
// End custom username highlighting -----
*/
/*
var apfools22 = {
  init: function() {
    if (!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
    	
   // var randomSeed = Math.floor(Math.random() * 50)
   // console.log(randomSeed);
      if (
        !$('body').hasClass('action-edit') &&
        !mw.storage.get('skeletonFired') // && randomSeed === 0
      ) {
        var $skeleton = document.createElement('audio');
        var $lootbox = document.createElement('img');

        $skeleton.src = '/w/images/9/93/User_Wookipan_theskeletonappears.wav';

        $lootbox.classList.add('lootbox');
        $lootbox.src = '/w/images/b/b8/User_Wookipan_lootcrate.png';
        $lootbox.title = 'Click me!';

        $('.apfools22').detach().prependTo('.mediawiki');
        $('.mediawiki').prepend($lootbox);

        $('.lootbox').click(function() {
          $('.lootbox').fadeOut();
          $('html, body').animate({scrollTop: '0px'}, 0).css('overflow', 'hidden');

          $skeleton.play();

          $($skeleton).on('ended', function() {
            window.location.reload(true);
          });

          $('.thewikiskeleton').fadeIn('slow');
          setTimeout(function() {
            $('.thewikiskeleton-bg').fadeIn(1000, function () {
              $('.apfools22 p').fadeIn(1000);
            });
          }, 2500);
          mw.storage.set('skeletonFired', 'true');
        });
      } else {
      	$('.apfools22').remove();
      }
    }
  },
};
$(apfools22.init);
*/