User:TeaLover1996/common.js: Difference between revisions

Content deleted Content added
TeaLover1996 (talk | contribs)
No edit summary
m Maintenance: Global identifier "$j" has been deprecated mw:ResourceLoader/Migration_guide_(users)#MediaWiki_1.17
 
(12 intermediate revisions by 2 users not shown)
Line 1:
importScript('User:Cameltrader/Advisor.js');
var npp_http;
-- Calculate the minimum-sized block of IP addresses that covers each
var npp_enabled;
-- IPv4 address entered in the arguments.
var npp_num_pages;
var npp_refresh;
var npp_num_idle_req;
var npp_curr_idle_req;
 
var npp_str_no_ajax = "There seems to be a problem using the NewPagePatrol script. Your browser is not supported. God Bless!";
var npp_str_box_title = "New pages";
var npp_str_box_title_updating = "New pages (updating)";
var npp_str_box_title_failed = "New pages (update failed)";
var npp_str_enable = "Enable this box";
var npp_str_disable = "Disable this box";
 
$( npp_init );
/* initalise */
local bit32 = require('bit32')
function npp_init() {
// allow user settings through
local function collection()
if (npp_enabled == null) {
-- Return a table to hold items.
npp_enabled = false;
return {
n = 0,}
if (npp_num_pages == null) {
add = function (self, item)
npp_num_pages = 10;
self.n = self.n + 1
}
self[self.n] = item
if (npp_refresh == null) {
end,
npp_refresh = 5;
join = function (self, sep)
}
return table.concat(self, sep)
if (npp_num_idle_req == null) {
end,
npp_num_idle_req = 60;
sort = function (self, comp)
}
table.sort(self, comp)
 
end,
// A few limits to be nice to the servers
}
if (npp_num_pages > 50) {
end
npp_num_pages = 50;
}
if (npp_num_pages < 1) {
npp_num_pages = 1;
}
if (npp_refresh < 2) {
npp_refresh = 2;
}
if (npp_num_idle_req > 1000) {
npp_num_idle_req = 1000;
}
if (npp_num_idle_req < 5) {
npp_num_idle_req = 5;
}
 
// get our cookie
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf("npp_show_box=");
if (c_start != -1) {
c_start = c_start + 13;
var c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
if (document.cookie.substring(c_start, c_end) == "yes") {
local function ipv4_string(ip_num)
npp_enabled = true;
-- Return a quad-dotted string equivalent to given 32-bit number.
} else {
local floor = math.floor
npp_enabled = false;
local quad = {}
for i = 1, 4 do }
}
quad[5 - i] = tostring(ip_num % 256)
}
ip_num = floor(ip_num / 256)
end
return table.concat(quad, '.')
end
// Either make a request or show nothing
local function ipv4_address(ip_str)
npp_curr_idle_req = 0;
-- Return number equivalent to IPv4 address given as a quad-dotted string, or
if (npp_enabled == true) {
-- return nil if invalid.
npp_ajax_request();
-- A redundant leading zero is an error because it is for an IP in octal.
} else {
local quad = collection()
npp_draw_disabled_box();
local s = ip_str:match('^%s*(.-)%s*$') .. '.'
}
for item in s:gmatch('(.-)%.') do
}
quad:add(item)
end
if quad.n == 4 then
local result = 0
for _, s in ipairs(quad) do
if s:match('^%d+$') then
local num = tonumber(s)
if num == 0 then
result = result * 256
elseif 0 < num and num <= 255 then
if s:match('^0') then
return nil
end
result = result * 256 + num
else
return nil
end
else
return nil
end
end
return result
end
return nil
end
/* init ajax */
local function ipv4_cidr(ipnums, from_size)
function npp_create_request() {
-- Return prefix, size (two numbers) for the smallest range that covers each IP,
try {
-- starting with the given size (a number from 0 to 32), or
npp_http = new XMLHttpRequest();
-- return nil if no single range works.
local arshift = bit32.arshift
local band = bit32.band
local result
for size = from_size, 32 do
local prefix
local mask = arshift(0x80000000, size - 1)
for i, v in ipairs(ipnums) do
if i == 1 then
prefix = band(v, mask)
else
if prefix ~= band(v, mask) then
return result, size - 1
end
end
end
result = prefix
end
return result, 32
end
} catch (e) {
local function IPblock(frame)
try {
-- Return wikitext to display the smallest IPv4 CIDR range that covers
npp_http = new ActiveXObject("Msxml2.XMLHTTP");
-- each IPv4 address given in the arguments, or error text.
} catch (e) {
-- An IP starting with '#' is ignored (commented out).
try {
local pframe = frame:getParent()
npp_http = new ActiveXObject("Microsoft.XMLHTTP");
local args = pframe.args
} catch (e) {
local ipnums = collection()
return false;
for i, arg in ipairs(args) do
}
for line in string.gmatch(arg .. '\n', '[\t ]*(.-)[\t\r ]*\n') do
}
if line ~= '' and line:sub(1, 1) ~= '#' then
}
local ip = ipv4_address(line)
if ip then
ipnums:add(ip)
else
error(string.format('Invalid IPv4 address: argument %d (%s)', i, line), 0)
end
end
end
end
if ipnums.n < 1 then
error('Need an IPv4 address', 0)
end
ipnums:sort()
local lines = collection()
lines:add('Sorted IP addresses:') -- at lines[1]
local previous
local dupcount = 0
for i, v in ipairs(ipnums) do
if previous == v then -- omit duplicates
dupcount = dupcount + 1
else
lines:add('#' .. ipv4_string(v))
end
previous = v
end
if dupcount > 0 then
lines[1] = 'Sorted IP addresses (after omitting some duplicates):'
end
lines:add('')
local from_size = tonumber(args.size) or 16
local prefix, size = ipv4_cidr(ipnums, from_size)
local text
if prefix then
if size == 32 then
text = 'Single IP address = ' .. ipv4_string(prefix)
else
text = string.format('Range (covers %d IP addresses) = %s', 2^(32 - size), ipv4_string(prefix) .. '/' .. size)
end
else
text = string.format('Error: No single CIDR range with size %d or more bits covers the IP addresses.', from_size)
end
lines:add(text)
return lines:join('\n')
end
npp_http.onreadystatechange = function() {
return { IPblock = IPblock }
if(npp_http.readyState == 4) npp_ajax_response();
}
return true;
}
/* make a request */
function npp_ajax_request() {
// if we have done too many requests, disable the box
npp_curr_idle_req++;
if (npp_curr_idle_req > npp_num_idle_req) {
npp_disable_box();
}
 
// check we are enabled
if (npp_enabled == false) return;
 
// firstly, inform the user
var cur_box = document.getElementById('p-newpages');
if (cur_box != null) {
cur_box.firstChild.firstChild.data = npp_str_box_title_updating;
}
if (npp_create_request () == false) {
if (cur_box != null) {
cur_box.firstChild.firstChild.data = npp_str_box_title_failed;
} else {
alert (npp_str_no_ajax);
}
}
// Get the current time
var dateobj = new Date();
var now = Math.floor(dateobj.getTime() / 1000.0);
 
// Go back 5 mins in time
now -= (60 * 5);
 
// Then make the request
npp_http.open("GET", "/w/api.php?action=query&format=xml&list=recentchanges&rcshow=!bot|!redirect&rctype=new&rcnamespace=0&rcprop=title|timestamp|ids|patrolled&rcstart=" + now + "&rclimit=" + npp_num_pages, true);
npp_http.send(null);
}
 
function npp_draw_disabled_box() {
if (mw.config.get('skin') == 'vector') {
npp_draw_disabled_box_vector();
} else {
npp_draw_disabled_box_monobook();
}
}
 
function npp_ajax_response() {
if (mw.config.get('skin') == 'vector') {
npp_ajax_response_vector();
} else {
npp_ajax_response_monobook();
}
 
// and do it again in 5 secs
setTimeout("npp_ajax_request()", npp_refresh * 1000);
}
 
function npp_disable_box() {
npp_enabled = false;
npp_draw_disabled_box();
document.cookie = "npp_show_box=no; path=/";
}
function npp_enable_box() {
npp_enabled = true;
npp_curr_idle_req = 0;
document.cookie = "npp_show_box=yes; path=/";
npp_ajax_request();
}
 
 
/* Draw disabled (monobook) */
function npp_draw_disabled_box_monobook() {
// Container div
var link_div = document.createElement('div');
link_div.className = 'pBody';
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portlet';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
// enable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
var a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_enable));
a.onclick = npp_enable_box;
p.appendChild(a);
link_div.appendChild(p);
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('column-one');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-search');
side_col.insertBefore(div, node);
}
}
 
/* Draw response (monobook) */
function npp_ajax_response_monobook() {
var items = npp_http.responseXML.getElementsByTagName('rc');
// create the div that holds all the newpage links
var link_div = document.createElement('div');
link_div.className = 'pBody';
var list = document.createElement('ul');
link_div.appendChild(list);
// populate the list with 10 links.
for (var i = 0; i < items.length; i++) {
var item_name = items[i].getAttribute('title');
var rcid = items[i].getAttribute('rcid');
var patrolled = items[i].getAttribute('patrolled') != null;
item_name = item_name.replace(/&/, "%26");
var item_url = mw.config.get('wgScript') + '?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
a = document.createElement('a');
a.setAttribute('href', item_url);
a.appendChild(document.createTextNode(item_name));
 
var li = document.createElement('li');
li.appendChild(a);
if (!patrolled) {
li.setAttribute('class', 'not-patrolled');
}
list.appendChild(li);
}
// Container div
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portlet';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
// disable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
p.style.textAlign = 'right';
a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_disable));
a.onclick = npp_disable_box;
p.appendChild(a);
link_div.appendChild(p);
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('column-one');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-search');
side_col.insertBefore(div, node);
}
}
 
 
/* Draw disabled box (vector skin) */
function npp_draw_disabled_box_vector() {
// Container div
var link_div = document.createElement('div');
link_div.className = 'body';
link_div.style.display = "block";
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portal collapsed';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
// enable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
var a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_enable));
a.onclick = npp_enable_box;
p.appendChild(a);
// add later
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('mw-panel');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-interaction');
side_col.insertBefore(div, node);
}
 
if( typeof $ != 'undefined' ) {
$('#p-newpages > h3').keydown( function( event ) {
if ( event.which == 13 /* Enter */ || event.which == 32 /* Space */ ) {
npp_toggle( $(this) );
}
} )
.mousedown( function() {
npp_toggle( $(this) );
$(this).blur();
return false;
} );
} else {
link_div.appendChild(p);
}
}
 
/* Draw response (vector skin) */
function npp_ajax_response_vector() {
var items = npp_http.responseXML.getElementsByTagName('rc');
// create the div that holds all the newpage links
var link_div = document.createElement('div');
link_div.className = 'body';
link_div.style.display = "block";
var list = document.createElement('ul');
link_div.appendChild(list);
// populate the list with 10 links.
for (var i = 0; i < items.length; i++) {
var item_name = items[i].getAttribute('title');
var rcid = items[i].getAttribute('rcid');
var patrolled = items[i].getAttribute('patrolled') != null;
item_name = item_name.replace(/&/, "%26");
var item_url = mw.config.get('wgScript') + '?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
a = document.createElement('a');
a.setAttribute('href', item_url);
//a.style.fontSize = 'x-small';
a.appendChild(document.createTextNode(item_name));
var li = document.createElement('li');
li.appendChild(a);
if (!patrolled) {
//li.setAttribute('class', 'not-patrolled');
li.setAttribute('style','background-color: #e7e7e7');
//li.style.color = '#555555';
 
}
list.appendChild(li);
}
// Container div
var div = document.createElement('div');
div.setAttribute('id', 'p-newpages');
div.className = 'portal expanded';
var heading = document.createElement('h3');
heading.appendChild(document.createTextNode(npp_str_box_title));
div.appendChild(heading);
div.appendChild(link_div);
 
// disable link
var p = document.createElement('p');
p.style.fontSize = 'x-small';
p.style.margin = '0px';
p.style.textAlign = 'left';
a = document.createElement('a');
a.appendChild(document.createTextNode(npp_str_disable));
a.onclick = npp_disable_box;
p.appendChild(a);
// add later
 
// now replace the div
var old_div = document.getElementById('p-newpages');
var side_col = document.getElementById('mw-panel');
if (old_div != null) {
side_col.replaceChild(div, old_div);
} else {
var node = document.getElementById('p-interaction');
side_col.insertBefore(div, node);
}
 
if( typeof $ != 'undefined' ) {
$('#p-newpages > h5').keydown( function( event ) {
if ( event.which == 13 /* Enter */ || event.which == 32 /* Space */ ) {
npp_toggle( $(this) );
}
} )
.mousedown( function() {
npp_toggle( $(this) );
$(this).blur();
return false;
} );
} else {
link_div.appendChild(p);
}
}
 
function npp_toggle( $element ) {
$.cookie( 'vector-nav-' + $element.parent().attr( 'id' ), $element.parent().is( '.collapsed' ) );
if( $element.parent().is('.collapsed') ) {
npp_enable_box();
} else {
npp_disable_box();
}
$element
.parent()
.toggleClass( 'expanded' )
.toggleClass( 'collapsed' )
.find( 'div.body' )
.slideToggle( 'fast' );
}