The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.1KB

  1. /*
  2. * options/user-js.js - allow user enter custom javascripts
  3. *
  4. * Copyright (c) 2014 Marcin Łabanowski <marcin@6irc.net>
  5. *
  6. * Usage:
  7. * $config['additional_javascript'][] = 'js/jquery.min.js';
  8. * $config['additional_javascript'][] = 'js/options.js';
  9. * $config['additional_javascript'][] = 'js/options/user-js.js';
  10. */
  11. +function(){
  12. var tab = Options.add_tab("user-js", "code", _("User JS"));
  13. var textarea = $("<textarea></textarea>").css({
  14. "font-size": 12,
  15. position: "absolute",
  16. top: 35, bottom: 35,
  17. width: "calc(100% - 20px)", margin: 0, padding: "4px", border: "1px solid black",
  18. left: 5, right: 5
  19. }).appendTo(tab.content);
  20. var submit = $("<input type='button' value='"+_("Update custom Javascript")+"'>").css({
  21. position: "absolute",
  22. height: 25, bottom: 5,
  23. width: "calc(100% - 10px)",
  24. left: 5, right: 5
  25. }).click(function() {
  26. localStorage.user_js = textarea.val();
  27. document.location.reload();
  28. }).appendTo(tab.content);
  29. var apply_js = function() {
  30. var proc = function() {
  31. $('.user-js').remove();
  32. $('script')
  33. .last()
  34. .after($("<script></script>")
  35. .addClass("user-js")
  36. .text(localStorage.user_js)
  37. );
  38. }
  39. if (/immediate()/.test(localStorage.user_js)) {
  40. proc(); // Apply the script immediately
  41. }
  42. else {
  43. $(proc); // Apply the script when the page fully loads
  44. }
  45. };
  46. var update_textarea = function() {
  47. if (!localStorage.user_js) {
  48. textarea.text("/* "+_("Enter here your own Javascript code...")+" */\n" +
  49. "/* "+_("Have a backup of your storage somewhere, as messing here\nmay render you this website unusable.")+" */\n" +
  50. "/* "+_("You can include JS files from remote servers, for example:")+" */\n" +
  51. 'load_js("http://example.com/script.js");');
  52. }
  53. else {
  54. textarea.text(localStorage.user_js);
  55. apply_js();
  56. }
  57. };
  58. update_textarea();
  59. // User utility functions
  60. window.load_js = function(url) {
  61. $('script')
  62. .last()
  63. .after($("<script></script>")
  64. .prop("type", "text/javascript")
  65. .prop("src", url)
  66. );
  67. };
  68. window.immediate = function() { // A dummy function.
  69. }
  70. }();