rightclickedit.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function setupRightClickEdit() {
  2. if (document.getElementsByTagName) {
  3. var spans = document.getElementsByTagName('span');
  4. for (var i = 0; i < spans.length; i++) {
  5. var el = spans[i];
  6. if(el.className == 'editsection') {
  7. addRightClickEditHandler(el);
  8. }
  9. }
  10. }
  11. }
  12. function addRightClickEditHandler(el) {
  13. for (var i = 0; i < el.childNodes.length; i++) {
  14. var link = el.childNodes[i];
  15. if (link.nodeType == 1 && link.nodeName.toLowerCase() == 'a') {
  16. var editHref = link.getAttribute('href');
  17. // find the enclosing (parent) header
  18. var prev = el.parentNode;
  19. if (prev && prev.nodeType == 1 &&
  20. prev.nodeName.match(/^[Hh][1-6]$/)) {
  21. prev.oncontextmenu = function(e) {
  22. if (!e) { e = window.event; }
  23. // e is now the event in all browsers
  24. var targ;
  25. if (e.target) { targ = e.target; }
  26. else if (e.srcElement) { targ = e.srcElement; }
  27. if (targ.nodeType == 3) { // defeat Safari bug
  28. targ = targ.parentNode;
  29. }
  30. // targ is now the target element
  31. // We don't want to deprive the noble reader of a context menu
  32. // for the section edit link, do we? (Might want to extend this
  33. // to all <a>'s?)
  34. if (targ.nodeName.toLowerCase() != 'a'
  35. || targ.parentNode.className != 'editsection') {
  36. document.location = editHref;
  37. return false;
  38. }
  39. return true;
  40. };
  41. }
  42. }
  43. }
  44. }
  45. hookEvent("load", setupRightClickEdit);