Skip to content

Instantly share code, notes, and snippets.

@IOZ
Last active November 29, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOZ/3ae3e5276524891f00aa to your computer and use it in GitHub Desktop.
Save IOZ/3ae3e5276524891f00aa to your computer and use it in GitHub Desktop.
Script, which allow you automatically update all album photos description.
(function() {
'use strict';
var VKPhotoDescriptionUpdater = {
isDone: false,
isCaptcha: false,
maxPhotos: null,
isReplace: false,
isAddToEnd: false,
text: null,
textToReplace: null,
currentText: null,
finalText: '',
counter: 0,
timer: 200,
sound: null,
/* UI */
vkPhotoUpdaterSettings: null,
vkPhotoUpdaterText: null,
vkPhotoUpdaterTextToReplace: null,
vkPhotoUpdaterIsReplace: null,
vkPhotoUpdaterRun: null,
vkPhotoUpdaterStat: null,
vkPhotoUpdaterCount: null,
init: function() {
this.maxPhotos = cur.pvData[cur.pvListId].length;
this.showUI();
this.vkPhotoUpdaterSettings = document.getElementById('vkPhotoUpdaterSettings');
this.vkPhotoUpdaterText = document.getElementById('vkPhotoUpdaterText');
this.vkPhotoUpdaterTextToReplace = document.getElementById('vkPhotoUpdaterTextToReplace');
this.vkPhotoUpdaterIsReplace = document.getElementById('vkPhotoUpdaterIsReplace');
this.vkPhotoUpdaterAddToEnd = document.getElementById('vkPhotoUpdaterAddToEnd');
this.vkPhotoUpdaterRun = document.getElementById('vkPhotoUpdaterRun');
this.vkPhotoUpdaterStat = document.getElementById('vkPhotoUpdaterStat');
this.vkPhotoUpdaterCount = document.getElementById('vkPhotoUpdaterCount');
this.vkPhotoUpdaterRestartSel = document.getElementById('vkPhotoUpdaterRestartSel');
this.vkPhotoUpdaterRestartBtn = document.getElementById('vkPhotoUpdaterRestartBtn');
this.sound = new Sound('mp3/bb2');
this.events();
},
events: function() {
this.vkPhotoUpdaterRun.addEventListener('click', this.runApp.bind(this));
this.vkPhotoUpdaterIsReplace.addEventListener('click', this.showReplaceBox.bind(this));
this.vkPhotoUpdaterRestartBtn.addEventListener('click', this.restart.bind(this));
},
runApp: function() {
this.text = this.vkPhotoUpdaterText.value;
this.textToReplace = this.vkPhotoUpdaterTextToReplace.value;
this.isReplace = this.vkPhotoUpdaterIsReplace.checked;
this.isAddToEnd = this.vkPhotoUpdaterAddToEnd.checked;
this.showStatistics();
this.updateDescription();
},
showReplaceBox: function() {
if (this.vkPhotoUpdaterTextToReplace.style.display == 'none') {
this.vkPhotoUpdaterTextToReplace.style.display = 'block';
} else {
this.vkPhotoUpdaterTextToReplace.style.display = 'none';
}
},
showStatistics: function() {
this.vkPhotoUpdaterSettings.style.display = 'none';
this.vkPhotoUpdaterStat.style.display = 'block';
},
updateDescription: function() {
if (this.isDone) { this.afterComplete(); return; }
this.editInline(event);
},
onTextUpdate: function() {
this.updateCounter();
if (!cur.pvTagger && checkEvent(event) === false) {
if (!this.isCaptcha) Photoview.show(false, cur.pvIndex + 1, event);
this.updateDescription();
if (cur.pvIndex + 1 == this.maxPhotos) { this.isDone = true; }
}
},
onTextShow: function() {
var self = this;
if (document.getElementById('pv_edit_text')) {
self.currentText = document.getElementById('pv_edit_text').value;
if (self.isReplace) {
if (self.currentText.length) {
self.finalText = self.currentText.replace(self.text, self.textToReplace);
}
} else {
if (self.isAddToEnd) {
self.finalText = self.currentText + ' ' + self.text;
} else {
self.finalText = self.text;
}
}
document.getElementById('pv_edit_text').value = self.finalText;
self.saveInline();
}
},
updateCounter: function() {
this.counter += 1;
this.vkPhotoUpdaterCount.innerHTML = this.counter;
},
afterComplete: function() {
this.vkPhotoUpdaterRestartSel.style.display = 'block';
},
restart: function() {
this.isDone = false;
this.counter = 0;
this.vkPhotoUpdaterCount.innerHTML = 0;
this.vkPhotoUpdaterSettings.style.display = 'block';
this.vkPhotoUpdaterStat.style.display = 'none';
this.vkPhotoUpdaterRestartSel.style.display = 'none';
},
saveInline: function() {
var self = this;
if (!cur.pvEditing) return;
removeEvent(ge('pv_edit_text'), 'blur');
var listId = cur.pvEditing[0], index = cur.pvEditing[1], ph = cur.pvData[listId][index], t = trim(val('pv_edit_text'));
if (geByClass1('pv_desc_edit', cur.pvDesc) && !t) return Photoview.cancelInline();
ajax.post('al_photos.php', {act: 'save_desc', photo: ph.id, hash: ph.hash, text: t}, {
onDone: function (text) {
ph.desc = text;
var shown = cur.pvShown && listId == cur.pvListId && index == cur.pvIndex;
if (!shown) return;
cur.pvEditing = false;
var d = domFC(cur.pvDesc);
val(d, text || ('<span class="pv_desc_edit">' + getLang('photos_edit_desc') + '</span>'));
d.onmouseover = text ? Photoview.descTT.pbind(d) : function () {
};
show(d);
re(domNS(d));
self.onTextUpdate();
}, progress: 'pv_inlineedit_prg'
});
},
editInline: function editInline(ev, noreq) {
var self = this;
if (((ev || window.event || {}).target || {}).tagName == 'A' || cur.pvEditing) return;
var listId = cur.pvListId, index = cur.pvIndex, ph = cur.pvData[listId][index], noreq = !ph.desc;
var onDone = function (text) {
if (!cur.pvShown || cur.pvListId != listId || cur.pvIndex != index || cur.pvEditing) return;
cur.pvEditing = [listId, index];
var mrg = '0px 0px 0px', taStyle = '';
if (browser.chrome || browser.msie) {
mrg = '0px 0px -5px';
taStyle = ' style="padding-bottom: 0px"';
} else if (browser.mozilla) {
mrg = '0px -1px 0px';
}
var el = cur.pvDesc.appendChild(ce('div', {
innerHTML: '\
<div style="margin: ' + mrg + '">\
<textarea id="pv_edit_text"' + taStyle + ' onkeydown="onCtrlEnter(event, Photoview.saveInline)" onkeyup="checkTextLength(cur.pvCaptionLimit, this, ge(\'pv_caption_warn\'));" placeholder="' + getLang('photos_edit_desc_intro') + '">' + text + '</textarea>\
<div id="pv_caption_warn"></div>\
</div>'
}, {display: 'none'})), txt = ge('pv_edit_text');
placeholderSetup(txt, {back: 1});
autosizeSetup(txt, {minHeight: 13});
setTimeout(function () {
show(el);
elfocus(txt);
addEvent(txt, 'blur', Photoview.saveInline);
hide(cur.pvDesc.firstChild);
self.onTextShow();
}, 1);
};
if (!noreq) {
ajax.post('al_photos.php', {act: 'edit_desc', photo: ph.id}, {
onDone: onDone,
progress: 'pv_inlineedit_prg'
});
} else {
onDone('');
}
},
showCaptchaBox: function() {
this.sound.play();
},
showUI: function() {
var boxC, cont, wrap, cont, opts, Mybox;
boxC = '<div class=""><div class="fc_tab_head"><a class="fc_tab_close_wrap fl_r"><div class="chats_sp fc_tab_close"></div></a><div class="fc_tab_title noselect">%title%</div></div><div id="fc_ctabs_cont"><div class="fc_ctab fc_ctab_active">%content%</div></div></div></div>';
cont = '';
cont += "<div class=\"fc_content_wrap\">";
cont += " <div class=\"fc_content\">";
cont += " <div class=\"wk_likes_likers_rows\">";
cont += " <div id=\"vkPhotoUpdaterSettings\">";
cont += " <textarea name=\"text\" id=\"vkPhotoUpdaterText\" placeholder=\"Текст\"><\/textarea>";
cont += " <textarea name=\"text\" id=\"vkPhotoUpdaterTextToReplace\" placeholder=\"Заменить на\" style=\"display: none;\"><\/textarea>";
cont += " <p>";
cont += " <label><input type=\"checkbox\" name=\"replace\" id=\"vkPhotoUpdaterIsReplace\" value=\"1\">Поиск и замена<\/label>";
cont += " <\/p>";
cont += " <p>";
cont += " <label><input type=\"checkbox\" name=\"add_to_end\" id=\"vkPhotoUpdaterAddToEnd\" value=\"1\">Добавить в конец<\/label>";
cont += " <\/p>";
cont += " <p>";
cont += " <button class=\"flat_button\" id=\"vkPhotoUpdaterRun\">Запустить<\/button>";
cont += " <\/p>";
cont += " <\/div>";
cont += " <div id=\"vkPhotoUpdaterStat\" style=\"display:none;\">";
cont += " Обновлено фото <b id=\"vkPhotoUpdaterCount\">0<\/b>";
cont += " <\/div>";
cont += " <div id=\"vkPhotoUpdaterRestartSel\" style=\"display:none;\">";
cont += " <p><b>Операция успешно завершена.<\/b><\/p>";
cont += " <button class=\"flat_button\" id=\"vkPhotoUpdaterRestartBtn\">Назад<\/button>";
cont += " <\/div>";
cont += " <p>";
cont += " <a href=\"http:\/\/vk.com\/topic-58308068_30320099\" target=\"_blank\">Посмотреть смайлики<\/a>";
cont += " <\/p>";
cont += " <style>";
cont += " .wk_likes_likers_rows { padding: 5px; }";
cont += " #vkPhotoUpdaterText, #vkPhotoUpdaterTextToReplace { margin: 5px auto; display: block; height: 40px; width: 100%; box-sizing: border-box;}";
cont += " <\/style>";
cont += " <\/div>";
cont += " <\/div>";
cont += "<\/div>";
wrap = se(rs(boxC, {
title: 'Статистика:',
content: cont
}));
cont = geByClass1('fc_content', wrap, 'div');
opts = {
movable: geByClass1('fc_tab_head', wrap),
hider: geByClass1('fc_tab_close_wrap', wrap, 'a'),
startHeight: 300,
startWidth: 300,
resizeableH: cont,
resize: true,
minH: 50
};
Mybox = new RBox(wrap, extend(opts, {}));
}
};
/* Override function and add custom callback */
if (typeof Photoview.saveInline == 'function') {
Photoview.saveInline = VKPhotoDescriptionUpdater.saveInline;
}
if (typeof Photoview.editInline == 'function') {
Photoview.editInline = VKPhotoDescriptionUpdater.editInline;
}
if (typeof showCaptchaBox == 'function') {
var showCaptchaBoxOriginal = showCaptchaBox;
showCaptchaBox = function() {
VKPhotoDescriptionUpdater.showCaptchaBox();
return showCaptchaBoxOriginal.apply(this, arguments);
};
}
VKPhotoDescriptionUpdater.init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment