Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
//<nowiki>
if (wgCanonicalNamespace == 'Marketplace' && !getCookie('ign').length) {
setCookie('ign',prompt('What is your in-game name?'),1000)
}
$(function(){
$('.shoptable th b').click(function() {
if ($(this).parents('.shoptable').is('.collapsed')) {
$(this).parents('.shoptable').removeClass('collapsed');
$(this).html('Close window')
} else {
$(this).parents('.shoptable').addClass('collapsed');
$(this).html('Open shop')
}
})
$('.money').attr('contenteditable','true').html(getCookie('shopMoney').length?getCookie('shopMoney'):'0').keyup(function() {
var cookieval = parseInt($(this).html().replace(/\s/g,''));
cookieval = cookieval?cookieval:'';
setCookie('shopMoney',cookieval,1000);
});
$('.info td+td+td').click(function() {$(this).find('.money').focus();}).attr('title','click to change the amount of money you have.');
if (wgCanonicalNamespace == 'Marketplace') {
$('h2').attr('data-section', function() {
return $(this).find('.editsection a').attr('href').replace(/.*?(\d+)$/, '$1');
})
}
})
$(function(){
$('td[colspan="2"][title]').click(function() {
$('.select').removeClass('select');
$(this).addClass('select');
var item = $(this).children('img').attr('alt').replace(/\.\w{2,4}$/,'');
var price = $(this).attr('title').replace('gp','');
var amount = parseInt($(this).children('.stock').html());
if (amount != 0) {
var amounts = '<b data-amount="1">1</b>'+(amount>=5?'<b data-amount="5">5</b>':'')+(amount>=10?'<b data-amount="10">10</b>':'')+(amount>=50?'<b data-amount="50">50</b>':'');
$('.buy').html('<td colspan="12" style="text-align:left;">'+item+': buy for '+price+'gp each</td><td colspan="5">Buy:'+amounts+'<b style="float:right;" data-amount="X">X</b></td><td></td>');
$('.sell').html('<td colspan="18">You do not have any of this item to sell</td>')
} else {
amount = parseInt($(this).find('.carried').html());
var amounts = '<b data-amount="1">1</b>'+(amount>=5?'<b data-amount="5">5</b>':'')+(amount>=10?'<b data-amount="10">10</b>':'')+(amount>=50?'<b data-amount="50">50</b>':'');
$('.sell').html('<td colspan="12" style="text-align:left;">'+item+': sell for '+price+'gp each</td><td colspan="5">Sell:'+amounts+'<b style="float:right;" data-amount="X">X</b></td><td></td>');
$('.buy').html('<td colspan="18">This item is currently not available to buy</td>');
}
var itemtd = this;
if (wgCanonicalNamespace == 'Marketplace' && getCookie('ign').length) {
$('.buy b, .sell b').click(function() {
var thisb = this;
submitOffer(itemtd,thisb)
});
}
})
$('.removeOffer').each(function() {
if (wgUserName == $(this).parents('table.offers').prev('.shoptable').prev('h2').children('.mw-headline').html() || $(this).is('tr.'+wgUserName+' .removeOffer')) {
$('.removeOffer').html('<a href="#" onclick="event.preventDefault();removeOffer(this);" title="Remove this offer (for example when the offer is completed or you reject the offer).">(remove)</a>')
}
})
if (wgCanonicalNamespace == 'Marketplace') {
var trs = $('table.offers tr+tr')
for (i=0;i<trs.length;i++) {
trs.eq(i).attr('data-pos',i);
}
}
})
function submitOffer(itemtd,thisb) {
var thistd = $(itemtd);
var amount = parseInt($(thisb).attr('data-amount'));console.log(amount);
amount = amount?amount:prompt('Type the number of items to buy and press enter');
if (!amount) {return false;}
amount = parseInt(amount);
if (!amount) { alert('The amount you entered is not a number.'); return false; }
var stock = parseInt(thistd.children('.stock').html());
stock = stock==0?parseInt(thistd.children('.carried').html()):stock;
var type = $(thisb).is('.buy b')?'buy':'sell';
if (amount > stock) { alert('The amount you entered is bigger than the '+(type=='buy'?'shop stocks':'buy offer')+'. Please enter an amount smaller than '+stock+'.'); return false;}
var item = thistd.children('img').attr('alt').replace(/\.\w{2,4}$/,'');
if (!confirm('Are you sure you want to place an offer to '+type+' '+amount+' '+item+'s?')) {return false;}
var section = thistd.parents('table.shoptable').prev('h2').attr('data-section');
var sectname = thistd.parents('table.shoptable').prev('h2').children('.mw-headline').html();
var sectid = thistd.parents('table.shoptable').prev('h2').children('.mw-headline').attr('id');
var price = parseInt(thistd.attr('title').replace('gp',''));
var template = '{{Marketplace offer|name='+getCookie('ign')+'|do='+type+'|item='+item+'|amount='+amount+'|price='+(amount*price)+'}}';
callAPI({
'action':'query',
'titles':wgPageName,
'prop':'info|revisions',
'intoken':'edit',
'rvprop':'content|ids',
'rvlimit':'1',
'indexpageids':'true',
'rvsection':section,
}, 'GET', function(response) {
var page = response.query.pages[response.query.pageids[0]];
var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';
content = content.replace(/\|\}\s*$/, template + '\n|}');
callAPI({
'action':'edit',
'title':wgPageName,
'section':section,
'token':page.edittoken,
'text':content,
'summary':'/*'+sectname+'*/ Adding offer to '+type+' items.',
'minor':'true',
}, 'POST', function(response) {
document.location.hash = sectid;
document.location.reload()
})
})
}
function removeOffer(thisrow) {
var pos = $(thisrow).parents('tr').attr('data-pos');
var section = $(thisrow).parents('table.offers').prev('table.shoptable').prev('h2').attr('data-section');
var sectid = $(thisrow).parents('table.offers').prev('table.shoptable').prev('h2').children('.mw-headline').attr('id');
var regex = new RegExp('({{Marketplace offers}}\n(.*\n){'+pos+'}).*\n');
callAPI({
'action':'query',
'titles':wgPageName,
'prop':'info|revisions',
'intoken':'edit',
'rvprop':'content|ids',
'rvlimit':'1',
'indexpageids':'true',
'rvsection':section,
}, 'GET', function(response) {
var page = response.query.pages[response.query.pageids[0]];
var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';
content = content.replace(regex, '$1');
callAPI({
'action':'edit',
'title':wgPageName,
'section':section,
'token':page.edittoken,
'text':content,
'summary':'Removing completed/rejected offer',
'minor':'true',
}, 'POST', function(response) {
document.location.hash = sectid;
document.location.reload()
})
})
}
/* CODE TO ADD NEW MARKETPLACE SHOPS */
$(function() {
$('#addnew').html('<button onclick="openAddShopForm();">Add or edit personal shop</button>');
})
function openAddShopForm() {
var selector = '.mw-headline:contains('+wgUserName+')'
if ($(selector).length) {
document.location.hash = $(selector).attr('id');
openEditShopForm($(selector).parent().attr('data-section'));
return false;
}
$('#addnew').after('<form action="javascript:addNewShop()" class="shopaddform"><div data-pos="1"><label>Item name: <input type="text" name="item1"></label> <label>Amount: <input type="text" name="amount1"></label> <label>Price: <input type="text" name="price1"></label></div><div class="buttons"><input type="submit" value="Submit changes"><button onclick="addItemRow()">Add another item</button></div></form>')
}
function addNewShop() {
var content = '{{Shop';
var pos;
$('.shopaddform div[data-pos]').each(function() {
pos = $(this).attr('data-pos');
content += '\n|'+ $(this).find('[name="item"]').val()+ '|a'+ pos + '='+ $(this).find('[name="amount"]').val()+ '|p'+ pos + '='+ $(this).find('[name="price"]').val();
})
content += '\n}}\n\n{{Marketplace offers}}\n|}';
callAPI({
'action':'query',
'titles':wgPageName,
'prop':'info',
'intoken':'edit',
'indexpageids':'true',
}, 'GET', function(response) {
var edittoken = response.query.pages[response.query.pageids[0]].edittoken;
callAPI({
'action':'edit',
'section':'new',
'title':wgPageName,
'token':edittoken,
'summary':wgUserName,
'minor':'true',
'text':content
}, 'POST', function(response) {
console.log(response)
});
});
}
function openEditShopForm(section) {
callAPI({
'action':'query',
'titles':wgPageName,
'prop':'info|revisions',
'intoken':'edit',
'rvprop':'content|ids',
'rvlimit':'1',
'indexpageids':'true',
'rvsection':section,
}, 'GET', function(response) {
var page = response.query.pages[response.query.pageids[0]];
var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';
var template = content.replace(/^(.*\n+{{Shop\n)((.|\n)+?)(\n+}}(.|\n)*)$/, '$2');
var params;
var lines = template.replace(/^\n+|\n+$/g,'').split('\n');
var divs = []
for (i=0;i<lines.length;i++) {
params = lines[i].replace(/^\|/,'').replace(/\|..=/g,'|').split('\|');
divs[i] = '<div data-pos="'+(i+1)+'"><label>Item name: <input type="text" name="item" value="'+params[0]+'"></label> <label>Amount: <input type="text" name="amount" value="'+params[1]+'"></label> <label>Price: <input type="text" name="price" value="'+params[2]+'"></label><a href="javascript:removeLine('+(i+1)+')">Remove</a></div>';
}
$('[data-section="'+section+'"]').after('<form action="javascript:editShop('+section+')" class="shopaddform">\n'+divs.join('\n')+'\n<tr><div><input type="submit" value="Submit changes"> <button onclick="addItemRow()">Add another item</button></div></form><br/>');
});
}
function removeLine(pos) {
$('.shopaddform div[data-pos="'+pos+'"]').remove();
$('.shopaddform div[data-pos]').each(function(i) {
$(this).attr('data-pos',i+1);
})
}
function addItemRow() {
var lastchild = $('.shopaddform div:last-child');
if (parseInt(lastchild.prev().attr('data-pos')) == 40) {
alert('You can only have a maximum of 40 items in a shop.');
return false;
}
if (lastchild.prev().find('input[name="item"]').val() != '' && lastchild.prev().find('input[name="amount"]').val() != '' && lastchild.prev().find('input[name="price"]').val() != '') {
lastchild.before('<div data-pos="'+ (parseInt(lastchild.prev().attr('data-pos'))+1) +'"><label>Item name: <input type="text" name="item"></label> <label>Amount: <input type="text" name="amount"></label> <label>Price: <input type="text" name="price"></label></div>');
} else {
alert('Please fill in all fields of the previous item before adding another row.')
}
}
function editShop(section, edittoken) {
var template = '{{Shop';
var pos;
$('.shopaddform div[data-pos]').each(function() {
pos = $(this).attr('data-pos');
template += '\n|'+ $(this).find('[name="item"]').val()+ '|a'+ pos + '='+ $(this).find('[name="amount"]').val()+ '|p'+ pos + '='+ $(this).find('[name="price"]').val();
})
template += '\n}}';
callAPI({
'action':'query',
'titles':wgPageName,
'prop':'info|revisions',
'intoken':'edit',
'rvprop':'content|ids',
'rvlimit':'1',
'indexpageids':'true',
'rvsection':section,
}, 'GET', function(response) {
var page = response.query.pages[response.query.pageids[0]];
var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';
content = content.replace(/{{Shop\n(.|\n)+?\n+}}/, template);
callAPI({
'action':'edit',
'title':wgPageName,
'section':section,
'token':page.edittoken,
'text':content,
'summary':'Editing shop stocks',
'minor':'true'
}, 'POST', function(response) {
document.location.reload()
})
})
}