User:MusikAnimal/importWatchlist.js

/*

INSTRUCTIONS:

Add the following line to your common.js:

importScript('User:MusikAnimal/importWatchlist.js'); // Linkback: User:MusikAnimal/importWatchlist.js

Please keep the linkback comment so I can see how many people use this)

There will be a new box at the top of your watchlist. Enter the username of the account whose watchlist you wish to import.

You will also need their watchlist token, which can be found in their Preferences under the Watchlist tab. Special:Preferences#mw-prefsection-watchlist

  • /

if($("#mw-watchlist-resetbutton").is(":visible")) {

$("#mw-watchlist-resetbutton").after(

"

" +

"Import watchlist" +

"

" +

"

 (can be found at Special:Preferences#mw-prefsection-watchlist)
" +

"

" +

"" +

"

"

);

$("#import_watchlist_form").submit(function(e) {

$("#import_watchlist_form").find("input,button").prop("disabled",true);

$("#import_watchlist_progress").html("Fetching watchlist...")

importWatchlistArray = [];

// do this craziness until Promises are more well supported

tempCount = 0;

tempFn = function() {

delete tempFn; // no dup calls

$("#import_watchlist_progress").html(importWatchlistArray.length + " watches queued for import");

for(var i=0; i

importWatches(importWatchlistArray.slice(i,i+49), function(ret) {

tempCount += 50;

if(tempCount < importWatchlistArray.length) {

$("#import_watchlist_progress").html("Working... " + tempCount + " of " + importWatchlistArray.length + " imported");

} else {

// done

$("#import_watchlist_progress").html("

Complete! " + importWatchlistArray.length + " pages imported to watchlist :)
");

delete tempCount; // why not

}

if(!ret) {

$("#import_watchlist_progress").after("

Error while importing batch " + parseInt(i/50) + ". Not all pages may have imported.
")

}

});

}

}

getWatchlist(null, tempFn);

e.preventDefault();

});

}

function importWatches(watches, fn) {

data = {

action : "watch",

format : "json",

titles : watches.join("|"),

token : mw.user.tokens.get('watchToken')

}

$.ajax({

type : "POST",

dataType : "json",

url : mw.util.wikiScript('api'),

data : data,

success : function(data) {

return fn(true);

},

error : function(data) {

return fn(false);

}

});

}

function getWatchlist(wrcontinue, fn) {

$.getJSON(mw.util.wikiScript('api') +

"?action=query&list=watchlistraw&wrowner="+$('#import_watchlist_username').val() +

"&wrtoken="+$('#import_watchlist_token').val() +

(wrcontinue ? "&wrcontinue="+wrcontinue : "") +

"&wrlimit=500&rawcontinue=&format=json", null, function(data) {

if(!data.watchlistraw) {

// error

$("#import_watchlist_progress").html("

Error fetching watchlist from user " + $("#import_watchlist_username").val() + ". Possible username/token mismatch.");

$("#import_watchlist_form").find("input,button").prop("disabled",false);

return false;

}

// first push to array

for(var i=0; i

importWatchlistArray.push(data.watchlistraw[i].title);

}

if(data.watchlistraw.length === 500) {

// may still be more to export

if(data['query-continue'] && data['query-continue'].watchlistraw && data['query-continue'].watchlistraw.wrcontinue) {

getWatchlist(data['query-continue'].watchlistraw.wrcontinue,function() {

if(data.watchlistraw.length < 500) {

// we're at the end

tempFn();

}

});

}

} else {

tempFn();

}

});

}

function getAjax(url) {

// Return a new promise.

return new Promise(function(resolve, reject) {

// Do the usual XHR stuff

var req = new XMLHttpRequest();

req.open('GET', url);

req.onload = function() {

// This is called even on 404 etc

// so check the status

if (req.status == 200) {

// Resolve the promise with the response text

resolve(req.response);

}

else {

// Otherwise reject with the status text

// which will hopefully be a meaningful error

reject(Error(req.statusText));

}

};

// Handle network errors

req.onerror = function() {

reject(Error("Network Error"));

};

// Make the request

req.send();

});

}