Fandom

Wikia Developers Wiki

Help with a script

  • Okay, I've been wanting to make a script to quickly warn a user on their wall in a click of a button. I have this code.

    document.createElement('<span class="wikia-button" onclick="WarnUser()">Warn this user</span>').prependTo('.tabs');
    function WarnUser(){
     var reason = prompt("What are you warning for?").log('#WallMessageTitle');
     'Warning: ' + reason + ' is against the rules. Don\'t do it again or you may get banned'.log('iframe');
    }
    

    But I don't see the button on the tabs. So this is what I need help with doing.

      Loading editor
    • Well, 2 reasons:

      1. Problems with WarnUser

      WarnUser has 2 lines, where each has a problem. The first line is:

      var reason = prompt("What are you warning for?").log('#WallMessageTitle');

      To the browser, this line takes the user input from the prompt window and executes its "log" method. If the user did submit some text, an error would occur, since strings don't have a "log" method (unless your wiki has set a String.prototype.log method). If the user presses "cancel" in the popup, that's even worse, since the returend value would be null, and attempting to set or get properties from null always throws an error. Same story with the second line. "log" isn't a method of strings.

      2. Problems with creating the markup

      document.createElement is meant to be passed a single argument- the element's tag name- it cannot be used for parsing HTML. If you want to parse some HTML, here are your options:

      /* suppose you've declared 'WarnUser' above already */
       
      // option 1: use jQuery
      $('<span class="wikia-button" onclick="WarnUser()">Warn this user</span>').prependTo('.tabs');
       
      // ###########################################
       
      // option 2: use native javascript
      var span = document.createElement("span"),
      	btn;
      span.className = "wikia-button";
      span.textContent = "Warn this user";
      Array.prototype.forEach.call(document.querySelectorAll(".tabs"), function(tab) {
      	btn = span.cloneNode(true);
      	btn.addEventListener("click", WarnUser);
      	tab.insertBefore(btn, tab.childNodes[0]);
      });
       
      // ###########################################
       
      // option 3: use DOMParser
      var btn = new DOMParser().parseFromString('<span class="wikia-button" onclick="WarnUser()">Warn this user</span>', "text/html");
      Array.prototype.forEach.call(document.querySelectorAll(".tabs"), function(tab) {
      	tab.insertBefore(btn.querySelector("span"), tab.childNodes[0]);
      });
        Loading editor
    • A Fandom user
        Loading editor
Give Kudos to this message
You've given this message Kudos!
See who gave Kudos to this message

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.