MediaWiki:Gadget-TimeBug.js
From Elwiki
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.
(function() {
var action = mw.config.get('wgAction');
var title = mw.config.get('wgPageName', '');
if(action === 'edit') {
if(window.localStorage && window.localStorage.getItem && window.localStorage.setItem && window.localStorage.removeItem) {
// =================================================
// Utility Functions
// =================================================
function getCache() {
var cache = localStorage.getItem('elwiki.timebug.cache') || '';
var map = {};
try {
map = JSON.parse(cache);
} catch (err) {
map = {};
}
return map;
}
function getCacheEntry() {
var cache = getCache();
return cache[title];
}
function setCache() {
var cache = getCache();
cache[title] = getWikiText();
localStorage.setItem('elwiki.timebug.cache', JSON.stringify(cache));
}
function deleteCache() {
var cache = getCache();
delete cache[title];
localStorage.setItem('elwiki.timebug.cache', JSON.stringify(cache));
}
function getWikiText() {
return document.getElementById('wpTextbox1').value;
}
function setWikiText(value) {
document.getElementById('wpTextbox1').value = value;
}
function getSkip() {
var skip = localStorage.getItem('elwiki.timebug.skip') || '';
return skip;
}
function setSkip() {
localStorage.setItem('elwiki.timebug.skip', 1);
}
function deleteSkip() {
localStorage.removeItem('elwiki.timebug.skip');
}
// Clean cache on save
document
.getElementById('wpSave')
.addEventListener('click', function() {
deleteCache();
setSkip();
});
// Save cache on unload
window
.addEventListener('beforeunload', function() {
if(!getSkip()) {
setCache();
}
deleteSkip();
});
// Replace wikitext on load
(function() {
var cache = getCacheEntry();
if(cache) {
if(confirm('You have cached wikitext. Do you want to restore?')) {
setWikiText(cache);
} else if(confirm('You did not use the cache. Do you want to discard?')) {
deleteCache();
setSkip();
}
}
})();
}
}
})();