|
const isDefined = (value) => typeof value !== 'undefined' && value !== null; |
|
|
|
/** |
|
* |
|
* @returns `true` if the field is set, `false` otherwise |
|
*/ |
|
const updateField = (data, field) => { |
|
const type = data[field].type; |
|
const value = data[field].content; |
|
|
|
if (!value) { |
|
return false; |
|
} |
|
|
|
const element = document.forms[0].__vue__._data.inputs.find((element) => |
|
element.$el.className.includes(`input-${field}`) |
|
); |
|
|
|
switch (type.toLowerCase()) { |
|
case 'prepend': |
|
element.internalValue = value + ' ' + element.internalValue; |
|
break; |
|
case 'append': |
|
element.internalValue += ' ' + value; |
|
break; |
|
case 'replace': |
|
element.internalValue = value; |
|
break; |
|
default: |
|
console.error(`'${type}' is not a valid way of updating a field.`); |
|
return false; |
|
} |
|
|
|
return true; |
|
}; |
|
|
|
const runBulk = async (data) => { |
|
const validation = validateData(data); |
|
|
|
if (validation) { |
|
console.error('There is an error in the data:\n\n' + validation); |
|
return; |
|
} |
|
|
|
console.time('bulk-edit'); |
|
|
|
try { |
|
const pause = async (seconds) => new Promise((r) => setTimeout(r, seconds * 1000)); |
|
|
|
let count = 0; |
|
|
|
do { |
|
let dirty = false; |
|
|
|
if (window.interrupt) { |
|
alert('Execution interrupted by user.'); |
|
delete window.interrupt; |
|
return; |
|
} |
|
|
|
for (let field of Object.keys(data)) { |
|
dirty |= updateField(data, field); |
|
} |
|
|
|
if (!dirty) { |
|
console.warn('No field was set. Nothing has changed.'); |
|
return; |
|
} |
|
|
|
const applyButton = document.querySelector('button.action-apply'); |
|
applyButton.click(); |
|
count++; |
|
|
|
const rightButton = document.querySelector('.v-toolbar__items .action-next'); |
|
if (rightButton.disabled) { |
|
break; |
|
} |
|
|
|
await pause(1); |
|
rightButton.click(); |
|
await pause(1); |
|
} while (true); |
|
|
|
const doneButton = document.querySelector('button.action-done'); |
|
doneButton.click(); |
|
|
|
console.info(`Bulk edited ${count} photos.`); |
|
} finally { |
|
console.timeEnd('bulk-edit'); |
|
} |
|
}; |
|
|
|
/** |
|
* Return LF delimited error message, or `null` if all is good. |
|
*/ |
|
const validateData = (data) => { |
|
if (!data) { |
|
return 'No data provided.'; |
|
} |
|
|
|
const error = []; |
|
|
|
if (isDefined(data.day?.content)) { |
|
const day = parseInt(data.day.content, 10); |
|
if (isNaN(day) || day < -1 || day > 31 || day === 0) { |
|
error.push('Day must be between 1 and 31. Set to -1 for "Unknown".'); |
|
} |
|
data.day.type = 'replace'; |
|
} |
|
|
|
if (isDefined(data.month?.content)) { |
|
const month = parseInt(data.month.content, 10); |
|
if (isNaN(month) || month < -1 || month > 12 || month === 0) { |
|
error.push('Month must be between 1 and 12. Set to -1 for "Unknown".'); |
|
} |
|
data.month.type = 'replace'; |
|
} |
|
|
|
if (isDefined(data.year?.content)) { |
|
const year = parseInt(data.year.content, 10); |
|
const currentYear = new Date().getFullYear(); |
|
if ((isNaN(year) || year < 1750 || year > currentYear) && year !== -1) { |
|
// 1750 is Photoprism defined year |
|
error.push('Year must be between 1750 and ' + currentYear + '. Set to -1 for "Unknown".'); |
|
} |
|
data.year.type = 'replace'; |
|
} |
|
|
|
return error.length > 0 ? error.join('\n') : null; |
|
}; |
Hi,
i've created a greasemonkey-script out of your gist.
see https://gist.github.com/boecko/e2d0effe7c61976c22e6bc0a8ee645c7
You can still run

runBulk
in the console, but it i've extended the edit-view of photoprismn.