573
edits
m (→Known Limitations: broken regexes: http://sourceforge.net/p/flightgear/mailman/message/31936560/) |
(added an *experimental* jquery dialog output + escapeHtml for code snippets quoting (not yet used)) |
||
Line 49: | Line 49: | ||
== The Script == | == The Script == | ||
// ==UserScript== | |||
// @name instant-cquotes | // @name instant-cquotes | ||
// @description automatically create proper wikimedia cquotes for sourceforge ml and forum text selections | // @description automatically create proper wikimedia cquotes for sourceforge ml and forum text selections | ||
Line 55: | Line 55: | ||
// @version 0.10 | // @version 0.10 | ||
// @icon http://wiki.flightgear.org/skins/common/images/icons-fg-135.png | // @icon http://wiki.flightgear.org/skins/common/images/icons-fg-135.png | ||
// @require http://code.jquery.com/jquery-1.11.1.min.js | |||
// @require https://code.jquery.com/ui/1.11.0/jquery-ui.min.js | |||
// @resource jqUI_CSS https://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css | |||
// @grant GM_addStyle | |||
// @grant GM_getResourceText | |||
// @match http://sourceforge.net/p/flightgear/mailman/* | // @match http://sourceforge.net/p/flightgear/mailman/* | ||
// @match http://forum.flightgear.org/* | // @match http://forum.flightgear.org/* | ||
// @copyright 2013+, FlightGear.org (bigstones, Philosopher & Hooray) | // @copyright 2013+, FlightGear.org (bigstones, Philosopher & Hooray) | ||
// ==/UserScript== | // ==/UserScript== | ||
var jqUI_CssSrc = GM_getResourceText ("jqUI_CSS"); | |||
GM_addStyle (jqUI_CssSrc); | |||
var debug = 0; | var debug = 0; | ||
/* content: | /* content: | ||
* - 'selection' supports only getSelectedText, will support getSelectedHtml | * - 'selection' supports only getSelectedText, will support getSelectedHtml | ||
Line 120: | Line 128: | ||
prepend("http://forum.flightgear.org")] | prepend("http://forum.flightgear.org")] | ||
} | } | ||
} | } | ||
}; | }; | ||
var OUTPUT = { | var OUTPUT = OutputMethods().msgbox; | ||
// shows a message box | |||
function OutputMethods() { | |||
var methods = { | |||
// shows a message box | |||
} | msgbox: function(msg) { | ||
if (window.prompt ("Copy to clipboard: Ctrl+C, Enter", msg) !== null) | |||
window.getSelection().removeAllRanges(); // deselect all text | |||
}, | |||
// XXX: experimental | |||
// show msg in a jQuery UI dialog | |||
jqueryDiag: function(msg) { | |||
var diagDiv = $('<div id="MyDialog"><textarea id="quotedtext" rows="10" cols="80" readonly>' + msg + '</textarea></div>'); | |||
var diagParam = { title: "Copy your quote with CTRL-C", | |||
modal: true, | |||
buttons: [{ text: "Select all", click: function () { $(this).select(); } }, | |||
{ text: "Cancel", click: function () { $('quotedtext').dialog("close"); } }] | |||
}; | |||
diagDiv.dialog(diagParam); | |||
} | |||
}; | |||
return methods; | |||
}; | }; | ||
////////////////////// | ////////////////////// | ||
// TRANSFORM OPERATORS | // TRANSFORM OPERATORS | ||
// prepend 'prefix' | // prepend 'prefix' | ||
function prepend(prefix) { | function prepend(prefix) { | ||
Line 140: | Line 169: | ||
}; | }; | ||
} | } | ||
// extract the first capture group in the regex (i.e. '(xxx)') | // extract the first capture group in the regex (i.e. '(xxx)') | ||
function extract(regex) { | function extract(regex) { | ||
Line 147: | Line 176: | ||
}; | }; | ||
} | } | ||
// replaces newlines with "unescaped" <br/>'s | // replaces newlines with "unescaped" <br/>'s | ||
function newline2br() { | function newline2br() { | ||
Line 154: | Line 183: | ||
}; | }; | ||
} | } | ||
// remove html comments (e.g. '<!-- this is a comment -->') | // remove html comments (e.g. '<!-- this is a comment -->') | ||
function removeComments() { | function removeComments() { | ||
Line 161: | Line 190: | ||
}; | }; | ||
} | } | ||
// converts html <a>...</a>'s to wiki links, internal if possible | // converts html <a>...</a>'s to wiki links, internal if possible | ||
function a2wikilink() { | function a2wikilink() { | ||
Line 172: | Line 201: | ||
// links to the wiki with custom text (we preserve it) | // links to the wiki with custom text (we preserve it) | ||
html = html.replace(/<a[^(?:href)]*href="https?:\/\/wiki.flightgear.org\/(.*?)".*?>(.*?)<\/a>/g, "[[$1|$2]]"); | html = html.replace(/<a[^(?:href)]*href="https?:\/\/wiki.flightgear.org\/(.*?)".*?>(.*?)<\/a>/g, "[[$1|$2]]"); | ||
// then the others | // then the others | ||
html = html.replace(/<a[^(?:href)]*href="(.*?)".*?>(.*?)<\/a>/g, "[$1 $2]"); | html = html.replace(/<a[^(?:href)]*href="(.*?)".*?>(.*?)<\/a>/g, "[$1 $2]"); | ||
Line 178: | Line 207: | ||
}; | }; | ||
} | } | ||
// converts embedded html images to external links, | // converts embedded html images to external links, | ||
// but shows them as little images if they're from the wiki | // but shows them as little images if they're from the wiki | ||
Line 196: | Line 225: | ||
} | } | ||
} | } | ||
// converts phpBB way of doing font style to the wiki way | // converts phpBB way of doing font style to the wiki way | ||
function phpBB_fontstyle2wikistyle() { | function phpBB_fontstyle2wikistyle() { | ||
Line 207: | Line 236: | ||
}; | }; | ||
} | } | ||
// converts (html) phpBB code blocks to wiki <syntaxhighlight> | // converts (html) phpBB code blocks to wiki <syntaxhighlight> | ||
// FIXME: not actually using the above tag, because the copied html has | // FIXME: not actually using the above tag, because the copied html has | ||
Line 217: | Line 246: | ||
}; | }; | ||
} | } | ||
// converts (html) phpBB quotes to simple wiki cquotes (author not cited, it'd get messy anyway) | // converts (html) phpBB quotes to simple wiki cquotes (author not cited, it'd get messy anyway) | ||
function phpBB_quote2cquote() { | function phpBB_quote2cquote() { | ||
Line 232: | Line 261: | ||
} | } | ||
} | } | ||
// escapes pipe characters that can be problematic inside a template. | // escapes pipe characters that can be problematic inside a template. | ||
// Must be used BEFORE html tags were converted, or they get messed up. | // Must be used BEFORE html tags were converted, or they get messed up. | ||
Line 242: | Line 271: | ||
} | } | ||
} | } | ||
// escapes the equal sign that can be problematic inside a template. | // escapes the equal sign that can be problematic inside a template. | ||
// Must be used AFTER html tags were converted, or they get messed up. | // Must be used AFTER html tags were converted, or they get messed up. | ||
Line 250: | Line 279: | ||
} | } | ||
} | } | ||
/////////////////// | /////////////////// | ||
// SCRIPT MAIN BODY | // SCRIPT MAIN BODY | ||
window.addEventListener('load', register); | window.addEventListener('load', register); | ||
function register() { | function register() { | ||
// check if we have a matching domain in the CONFIG hash | // check if we have a matching domain in the CONFIG hash | ||
Line 263: | Line 292: | ||
} | } | ||
} | } | ||
// main function | // main function | ||
function instantCquote() { | function instantCquote() { | ||
if(getSelectedText() === "") return; | if(getSelectedText() === "") return; | ||
var profile = CONFIG[window.location.hostname]; | var profile = CONFIG[window.location.hostname]; | ||
var parent = getSelectionParent(); | var parent = getSelectionParent(); | ||
var info = parent ? extractInfo(profile, parent) : extractInfoFallback(); | var info = parent ? extractInfo(profile, parent) : extractInfoFallback(); | ||
if (info) { | if (info) { | ||
var cquote = createCquote(info); | var cquote = createCquote(info); | ||
OUTPUT | OUTPUT(cquote); | ||
} else { | } else { | ||
alert("info == null"); | alert("info == null"); | ||
} | } | ||
} | } | ||
function extractInfoFallback() { // XXX untested | function extractInfoFallback() { // XXX untested | ||
var info = {}; | var info = {}; | ||
Line 290: | Line 319: | ||
return info; | return info; | ||
} | } | ||
function extractInfo(profile, parent) { | function extractInfo(profile, parent) { | ||
var info = {}; | var info = {}; | ||
for (var field in profile) { | for (var field in profile) { | ||
if (!profile.hasOwnProperty(field)) continue; | if (!profile.hasOwnProperty(field)) continue; | ||
// this part gets the data, both for field and content (i.e. text, html) | |||
var fieldInfo = extractFieldInfo(profile, parent, field); | |||
if (debug) alert("pre trans:\n" + field + " = " + fieldInfo); | |||
var transform = profile[field].transform; | |||
if(transform) fieldInfo = applyTransformations(fieldInfo, transform); | |||
if (debug) alert("post trans:\n" + field + " = " + fieldInfo); | |||
info[field] = fieldInfo; | |||
} | } | ||
return info; | return info; | ||
} | } | ||
function extractFieldInfo(profile, parent, field) { | function extractFieldInfo(profile, parent, field) { | ||
if (field != "content") { | if (field != "content") { | ||
Line 324: | Line 353: | ||
} | } | ||
} | } | ||
function applyTransformations(fieldInfo, trans) { | function applyTransformations(fieldInfo, trans) { | ||
if(typeof trans === "function") { | if(typeof trans === "function") { | ||
Line 337: | Line 366: | ||
} | } | ||
} | } | ||
function createCquote(info) { | function createCquote(info) { | ||
//TODO: add template string to profile | //TODO: add template string to profile | ||
Line 350: | Line 379: | ||
return template; | return template; | ||
} | } | ||
function createForumQuote(info) { | function createForumQuote(info) { | ||
//TODO: add template string to profile | //TODO: add template string to profile | ||
Line 358: | Line 387: | ||
return template; | return template; | ||
} | } | ||
function nowiki(text) { | function nowiki(text) { | ||
return "<nowiki>" + text + "</nowiki>"; | return "<nowiki>" + text + "</nowiki>"; | ||
} | } | ||
//////////////////// | //////////////////// | ||
// UTILITY FUNCTIONS | // UTILITY FUNCTIONS | ||
// IE8 compat. function | // IE8 compat. function | ||
function getSelectionParent() { | function getSelectionParent() { | ||
Line 372: | Line 400: | ||
// anchorNode is the textnode, parentNode is the parent html element | // anchorNode is the textnode, parentNode is the parent html element | ||
return document.getSelection().anchorNode.parentNode; | return document.getSelection().anchorNode.parentNode; | ||
} else if (document.selection) { // for IE <= v8 - XXX: not tested! | } else if (document.selection) { // for IE <= v8 - XXX: not tested! | ||
return document.selection.createRange().parentElement(); | return document.selection.createRange().parentElement(); | ||
} | } | ||
} | } | ||
// IE8 compat. function | // IE8 compat. function | ||
function getSelectedText() { | function getSelectedText() { | ||
if(document.getSelection) { // for modern, standard compliant browsers | if(document.getSelection) { // for modern, standard compliant browsers | ||
return document.getSelection().toString(); | return document.getSelection().toString(); | ||
} else if (document.selection) { // for IE <= v8 - XXX: not tested! | } else if (document.selection) { // for IE <= v8 - XXX: not tested! | ||
return document.selection.createRange().text; | return document.selection.createRange().text; | ||
} | } | ||
} | } | ||
// IE8 compat. function (copied from http://stackoverflow.com/a/6668159 ) | // IE8 compat. function (copied from http://stackoverflow.com/a/6668159 ) | ||
function getSelectedHtml() { | function getSelectedHtml() { | ||
Line 405: | Line 433: | ||
} | } | ||
} | } | ||
return html; | |||
} | } | ||
// copied from http://stackoverflow.com/a/2631931 | // copied from http://stackoverflow.com/a/2631931 | ||
function getXPathTo(element) { | function getXPathTo(element) { | ||
Line 414: | Line 442: | ||
if (element === document.body) | if (element === document.body) | ||
return element.tagName; | return element.tagName; | ||
var ix= 0; | var ix= 0; | ||
var siblings = element.parentNode.childNodes; | var siblings = element.parentNode.childNodes; | ||
Line 424: | Line 452: | ||
ix++; | ix++; | ||
} | } | ||
} | |||
// copied from http://stackoverflow.com/a/12034334 | |||
var entityMap = { | |||
"&": "&", | |||
"<": "<", | |||
">": ">", | |||
'"': '"', | |||
"'": ''', | |||
"/": '/', | |||
"\n": "<br/>" | |||
}; | |||
function escapeHtml(string) { | |||
return String(string).replace(/[&<>"'\/]|[\n]/g, function (s) { | |||
return entityMap[s]; | |||
}); | |||
}</syntaxhighlight> | }</syntaxhighlight> | ||
[[Category:Wiki maintenance]] | [[Category:Wiki maintenance]] |
edits