Files
xxx/publish/utilities.js
T
tim a9daa6a792 Replace TinyMCE 4.2.8 with a small local editor.
TinyMCE 4.2.8 has known XSS bugs. publish/editor.js has the same toolbar
and writes the same HTML. The textarea still posts the story.
2026-09-25 22:58:05 +02:00

113 lines
4.1 KiB
JavaScript

function showHide(show) {
if (show == 'text') {
var hide = 'upload';
} else if (show == 'upload') {
var hide = 'text';
} else {
return false;
}
if (show == 'text') {
document.getElementById(hide+'parent').style.display = "none";
document.getElementById(show+'parent').style.display = "block";
document.getElementById('wordcounter').style.display = "block";
if (window.storyEditor)
storyEditor.focus();
} else if (show == 'upload') {
document.getElementById('wordcounter').style.display = "none";
document.getElementById(hide+'parent').style.display = "none";
document.getElementById(show+'parent').style.display = "block";
}
return true;
}
// GLOBAL
var is_delete = 0;
function do_delete() {
if (confirm('Are you certain you wish to delete this post?')) {
is_delete = 1;
return true;
} else
return false;
}
function review() {
var title = document.getElementById('title');
var author = document.getElementById('author');
var email = document.getElementById('email');
var upload = document.getElementById('upload');
var uploaded = document.getElementById('uploaded');
var remove_img = document.getElementById('remove_img');
var txtcnt = wc(document.getElementById('story').value);
if (is_delete) return true;
if (!title.value) {
alert("Please add a title to your submission; one makes everything much prettier.");
return false;
} if (!author.value) {
alert("You haven't added an author. You can use a pseudonym if you like.");
return false;
} if (!email.value) {
if (!confirm("You haven't added an email address.\n\nThis will not be published. It will only be used by editors if we need to contact you.\n\nClick 'cancel' if you would like to go back and add one now."))
return false;
} if ((txtcnt == 0) && (upload.value == '') && ((!remove_img || remove_img.value == 1) || (!uploaded || uploaded.value == ''))) {
alert("You haven't submitted anything (not even a picture!).");
return false;
} else if ((txtcnt != '100') && (upload.value == '') && ((!remove_img || remove_img.value == 1) || (!uploaded || uploaded.value == ''))) {
if (!confirm("Your submission doesn't appear to be 100 words long. I'll continue anyway, but you may want to review."))
return false;
}
return true;
}
// GLOBAL
if (document.images) {
trashcan_img = new Image(16,22);
trashcan_img.src="/graphics/trashcan-open.png";
}
function toggle_remove() {
var virtual_checkbox = document.getElementById('remove_img');
var img = document.getElementById('remove_img_icon');
if(virtual_checkbox.value == 0) {
virtual_checkbox.value=1;
img.src='/graphics/trashcan-open.png'
img.title = 'Image is marked for removal.';
} else {
virtual_checkbox.value=0;
img.src='/graphics/trashcan.png'
img.title = 'Image has been restored; it will not be removed.';
}
}
// Show the number of words in the story (publish/editor.inc.php calls
// this when the story changes)
function updateWordCount() {
var story = document.getElementById("story");
var wordcounter = document.getElementById("wordcounter");
if (!story || !wordcounter)
return true;
var cnt = wc(story.value);
var words = (cnt == 1) ? " word" : " words";
wordcounter.innerHTML = cnt + words;
wordcounter.className = "counter";
if (cnt == 100) wordcounter.className += " yes";
if (cnt > 100) wordcounter.className += " no";
return true;
}
function wc (txt) {
txt = txt.replace(/(<br( \/)?>)/ig," "); // replace ending <br>s with spaces
txt = txt.replace(/(<([^>]+)>)/ig,""); //strip HTML
txt = txt.replace(/(\s?[a-zA-Z0-9]+)\s?(?:&.dash;\s?|-(?:-+\s?|\s))([a-zA-Z0-9]+)/g,"$1 $2"); //strip mdashes
txt = txt.replace(/\s[^a-zA-Z0-9]+\s/g," "); //strip punctuation
txt = txt.replace(/\s*((\S+\s*)*)/, "$1"); //ltrim
txt = txt.replace(/((\s*\S+)*)\s*/, "$1"); //rtrim
txt = txt.replace(/(&nbsp;)/g, " "); //empty paragraphs and non-breaking spaces
txt = txt.replace(/(^\s+|\s+$)/, " "); // Remove trailing spaces
if (txt.match(/^ *$/))
return 0;
var cnt = txt.split(/\s+/g);
return cnt.length;
}