Advertisement
moonlit

MAL Custom Tags

Jan 1st, 2012
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copyright (c) 2011, Alexander Shevchenko (alex.shevchenko@inbox.lv)
  2. // Released under the GPL license
  3. // http://www.gnu.org/copyleft/gpl.html
  4. //
  5. // Changes from version 1.1:
  6. //    New:
  7. //     - An expandable list at the top of the page with all available
  8. //       different custom tags in the selected status.
  9. //       Tags are sorted alphabetically.
  10. //       List shows up only when at least one tag is available.
  11. //     - A link to reload the page without a tag, but preserving status and order.
  12. //       Link shows up only when the tag parameter is present.
  13. //     - You can style the above two novelties and each of their components individually
  14. //       by editing the following stylesheet and using it in your MAL custom list style:
  15. //       http://alexsh.fileave.com/devel/web/userscript/mal_tags/stylesheets/mal_tags_v1_2.css
  16. //    Improvements:
  17. //     - Now also works with older browser versions (Firefox 3.0+, Opera 9.52+, Chrome, Midori).
  18. //     - A lot of optimization.
  19. //
  20. // This is a cross-browser script. Do any changes at your own risk.
  21. //
  22. // Enjoy!
  23. //
  24. // ==UserScript==
  25. // @name           MAL tags
  26. // @version        1.2
  27. // @namespace      http://myanimelist.net
  28. // @description    Temporary userscript for custom tags feature in MAL lists
  29. // ==/UserScript==
  30.  
  31. if (RegExp("(?:myanimelist\\.net\\/)(?:animelist|mangalist|\\/list\\.php)","i").test(location.href)) {
  32.     var getURLParameter = function(pName) {
  33.         var pValue = RegExp("&"+pName+"=([^&#]*)").exec(location.href);
  34.         if (pValue===null) return "";
  35.         else return decodeURIComponent(pValue[1]);
  36.     };
  37.     var hasClass = function(ele,cls) {
  38.         return RegExp("(?:\\s|^)"+cls+"(?:\\s|$)").test(ele.className);
  39.     };
  40.     var addClass = function(ele,cls) {
  41.         if (!hasClass(ele,cls)) {
  42.             if (ele.className) ele.className+=(" "+cls);
  43.             else ele.className=cls;
  44.         }
  45.     };
  46.     var testNode = document.createElement("p");
  47.     testNode.appendChild(document.createTextNode("test"));
  48.     if (testNode.textContent) {
  49.         var getText = function(node) {
  50.             if (node.textContent) return node.textContent;
  51.             return "";
  52.         };
  53.         var setText = function(node,text) { node.textContent=text; };
  54.     } else if (testNode.innerText) {
  55.         var getText = function(node) {
  56.             if (node.innerText) return node.innerText;
  57.             return "";
  58.         };
  59.         var setText = function(node,text) { node.innerText=text; };
  60.     } else {
  61.         var getText = function(node) { return ""; };
  62.         var setText = function(node,text) {};
  63.     }
  64.     var getNextSibling = function(ele) {
  65.         var x = ele.nextSibling;
  66.         while (x.nodeType!==1) x=x.nextSibling;
  67.         return x;
  68.     };
  69.     var toggleElement = function(ele,val1,val2) {
  70.         if (ele.style.display===val1) ele.style.display=val2;
  71.         else ele.style.display=val1;
  72.     };
  73.     var toggleText = function(node,text1,text2) {
  74.         if (getText(node)===text1) setText(node,text2);
  75.         else setText(node,text1);
  76.     };
  77.     var toggleTitle = function(ele,title1,title2) {
  78.         if (ele.title===title1) ele.title=title2;
  79.         else ele.title=title1;
  80.     };
  81.     var malTags = function() {
  82.         var currentTag = getURLParameter("tag");
  83.         var currentStatus = getURLParameter("status");
  84.         var currentOrder = getURLParameter("order");
  85.         var listSurround = document.getElementById("list_surround");
  86.         var i, j, k, tables = listSurround.getElementsByTagName("table"), spans = [];
  87.         for (i=0; i<tables.length; i++) {
  88.             spans.push(tables[i].getElementsByTagName("span"));
  89.         }
  90.         var tablesWithEntries = [];
  91.         if (currentTag || currentStatus || currentOrder) {
  92.             if (currentTag) {
  93.                 for (i=0; i<tables.length; i++) {
  94.                     for (j=0; j<spans[i].length; j++) {
  95.                         if (spans[i][j].id.search("tagLinks")===0) {
  96.                             tablesWithEntries.push([tables[i],spans[i][j].getElementsByTagName("a")]);
  97.                             break;
  98.                         }
  99.                     }
  100.                     if (tables[i].rows.length && tables[i].rows[0].cells.length &&
  101.                         hasClass(tables[i].rows[0].cells[0],"category_totals")) {
  102.                         if (tablesWithEntries.length) {
  103.                             addClass(tablesWithEntries[tablesWithEntries.length-1][0],"category_last");
  104.                         }
  105.                     }
  106.                 }
  107.             } else {
  108.                 for (i=0; i<tables.length; i++) {
  109.                     for (j=0; j<spans[i].length; j++) {
  110.                         if (spans[i][j].id.search("tagLinks")===0) {
  111.                             tablesWithEntries.push([tables[i],spans[i][j].getElementsByTagName("a")]);
  112.                             break;
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             if (currentStatus) {
  118.                 for (i=0; i<tablesWithEntries.length; i++) {
  119.                     for (j=0; j<tablesWithEntries[i][1].length; j++) {
  120.                         tablesWithEntries[i][1][j].href+=("&status="+currentStatus);
  121.                     }
  122.                 }
  123.             }
  124.             if (currentOrder) {
  125.                 for (i=0; i<tablesWithEntries.length; i++) {
  126.                     for (j=0; j<tablesWithEntries[i][1].length; j++) {
  127.                         tablesWithEntries[i][1][j].href+=("&order="+currentOrder);
  128.                     }
  129.                 }
  130.             }
  131.             if (currentTag) {
  132.                 var hasNumColumn = false;
  133.                 if (tablesWithEntries.length) {
  134.                     if (!tablesWithEntries[0][0].rows[0].cells[0].getElementsByTagName("a").length) {
  135.                         hasNumColumn=true;
  136.                     }
  137.                 }
  138.                 var l, a = 0, tagText;
  139.                 if (hasNumColumn) {
  140.                     for (i=0; i<tablesWithEntries.length; i++) {
  141.                         if (!tablesWithEntries[i][1].length) {
  142.                             tablesWithEntries[i][0].style.display="none";
  143.                         } else {
  144.                             for (j=0; j<tablesWithEntries[i][1].length; j++) {
  145.                                 tagText=getText(tablesWithEntries[i][1][j]);
  146.                                 if (tagText===currentTag) {
  147.                                     a+=1;
  148.                                     setText(tablesWithEntries[i][0].rows[0].cells[0],a);
  149.                                     for (k=0; k<tablesWithEntries[i][0].rows.length; k++) {
  150.                                         for (l=0; l<tablesWithEntries[i][0].rows[k].cells.length; l++) {
  151.                                             tablesWithEntries[i][0].rows[k].cells[l].className="td"+(2-a%2);
  152.                                         }
  153.                                     }
  154.                                     break;
  155.                                 }
  156.                                 if (j===tablesWithEntries[i][1].length-1) {
  157.                                     tablesWithEntries[i][0].style.display="none";
  158.                                 }
  159.                             }
  160.                         }
  161.                         if (hasClass(tablesWithEntries[i][0],"category_last")) a=0;
  162.                     }
  163.                 } else {
  164.                     for (i=0; i<tablesWithEntries.length; i++) {
  165.                         if (!tablesWithEntries[i][1].length) {
  166.                             tablesWithEntries[i][0].style.display="none";
  167.                         } else {
  168.                             for (j=0; j<tablesWithEntries[i][1].length; j++) {
  169.                                 tagText=getText(tablesWithEntries[i][1][j]);
  170.                                 if (tagText===currentTag) {
  171.                                     a+=1;
  172.                                     for (k=0; k<tablesWithEntries[i][0].rows.length; k++) {
  173.                                         for (l=0; l<tablesWithEntries[i][0].rows[k].cells.length; l++) {
  174.                                             tablesWithEntries[i][0].rows[k].cells[l].className="td"+(2-a%2);
  175.                                         }
  176.                                     }
  177.                                     break;
  178.                                 }
  179.                                 if (j===tablesWithEntries[i][1].length-1) {
  180.                                     tablesWithEntries[i][0].style.display="none";
  181.                                 }
  182.                             }
  183.                         }
  184.                         if (hasClass(tablesWithEntries[i][0],"category_last")) a=0;
  185.                     }
  186.                 }
  187.                 var dropTagTable = tables[0].cloneNode(false);
  188.                 dropTagTable.id="drop_tag_table";
  189.                 dropTagTable.style.cssFloat="right";
  190.                 dropTagTable.style.width="auto";
  191.                 dropTagTable.style.margin="12px 0px 0px";
  192.                 var dropTagRow = tables[0].rows[0].cloneNode(false);
  193.                 var dropTagCell = tables[0].rows[0].cells[0].cloneNode(false);
  194.                 dropTagCell.className="td2";
  195.                 dropTagCell.style.width="auto";
  196.                 var dropTagLink = document.createElement("a");
  197.                 dropTagLink.href=encodeURI(decodeURIComponent(location.href).replace("&tag="+currentTag,""));
  198.                 dropTagLink.title="Reload the page without a tag";
  199.                 setText(dropTagLink,"drop tag");
  200.                 dropTagCell.appendChild(dropTagLink);
  201.                 dropTagRow.appendChild(dropTagCell);
  202.                 dropTagTable.appendChild(dropTagRow);
  203.                 listSurround.insertBefore(dropTagTable,getNextSibling(tables[0]));
  204.             }
  205.         } else {
  206.             for (i=0; i<tables.length; i++) {
  207.                 for (j=0; j<spans[i].length; j++) {
  208.                     if (spans[i][j].id.search("tagLinks")===0) {
  209.                         tablesWithEntries.push([tables[i],spans[i][j].getElementsByTagName("a")]);
  210.                         break;
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.         var tagLinkText, allTagLinks = [];
  216.         for (i=0; i<tablesWithEntries.length; i++) {
  217.             for (j=0; j<tablesWithEntries[i][1].length; j++) {
  218.                 tagLinkText=getText(tablesWithEntries[i][1][j]);
  219.                 for (k=0; k<allTagLinks.length; k++) {
  220.                     if (getText(allTagLinks[k])===tagLinkText) break;
  221.                 }
  222.                 if (k===allTagLinks.length) {
  223.                     allTagLinks.push(tablesWithEntries[i][1][j].cloneNode(true));
  224.                 }
  225.             }
  226.         }
  227.         if (allTagLinks.length) {
  228.             allTagLinks.sort();
  229.             var customTagsTable = tables[0].cloneNode(false);
  230.             customTagsTable.id="custom_tags_table";
  231.             customTagsTable.style.cssFloat="left";
  232.             customTagsTable.style.width="70%";
  233.             customTagsTable.style.margin="12px 0px 0px 15%";
  234.             customTagsTable.style.borderCollapse="collapse";
  235.             var customTagsHeader = tables[0].rows[0].cloneNode(false);
  236.             customTagsHeader.style.cursor="pointer";
  237.             var customTagsContent = customTagsHeader.cloneNode(false);
  238.             customTagsContent.style.display="none";
  239.             customTagsHeader.id="custom_tags_header";
  240.             customTagsContent.id="custom_tags_content";
  241.             var customTagsHeader1 = tables[0].rows[0].cells[0].cloneNode(false);
  242.             var customTagsHeader2 = tables[0].rows[0].cells[tables[0].rows[0].cells.length-1].cloneNode(false);
  243.             customTagsHeader1.className=customTagsHeader2.className="status_selected";
  244.             customTagsHeader1.style.width="2em";
  245.             customTagsHeader2.style.width="auto";
  246.             var customTagsHeader1Link = document.createElement("a");
  247.             customTagsHeader1Link.name=customTagsHeader1Link.id="custom_tags_toggle1";
  248.             customTagsHeader1Link.title="Show custom tags";
  249.             setText(customTagsHeader1Link,"+");
  250.             var customTagsHeader2Link = document.createElement("a");
  251.             customTagsHeader2Link.name=customTagsHeader2Link.id="custom_tags_toggle2";
  252.             customTagsHeader2Link.title="Show custom tags";
  253.             setText(customTagsHeader2Link,"Available Custom Tags");
  254.             var customTagsContent1 = tables[0].rows[0].cells[0].cloneNode(false);
  255.             customTagsContent1.className="td2";
  256.             customTagsContent1.colSpan="2";
  257.             customTagsContent1.style.width="auto";
  258.             customTagsContent1.style.whiteSpace="normal";
  259.             customTagsHeader1.appendChild(customTagsHeader1Link);
  260.             customTagsHeader2.appendChild(customTagsHeader2Link);
  261.             customTagsContent1.appendChild(allTagLinks[0]);
  262.             for (i=1; i<allTagLinks.length; i++) {
  263.                 customTagsContent1.appendChild(document.createTextNode(", "));
  264.                 customTagsContent1.appendChild(allTagLinks[i]);
  265.             }
  266.             customTagsHeader.appendChild(customTagsHeader1);
  267.             customTagsHeader.appendChild(customTagsHeader2);
  268.             customTagsContent.appendChild(customTagsContent1);
  269.             customTagsTable.appendChild(customTagsHeader);
  270.             customTagsTable.appendChild(customTagsContent);
  271.             listSurround.insertBefore(customTagsTable,getNextSibling(tables[0]));
  272.             if (customTagsHeader.addEventListener) {
  273.                 customTagsHeader.addEventListener('click',
  274.                     function() {
  275.                         toggleElement(customTagsContent,"none","table-row");
  276.                         toggleText(customTagsHeader1Link,"+","-");
  277.                         toggleTitle(customTagsHeader1Link,"Show custom tags","Hide custom tags");
  278.                         toggleTitle(customTagsHeader2Link,"Show custom tags","Hide custom tags");
  279.                     },false);
  280.             } else if (customTagsHeader.attachEvent) {
  281.                 customTagsHeader.attachEvent("onclick",
  282.                     function() {
  283.                         toggleElement(customTagsContent,"none","table-row");
  284.                         toggleText(customTagsHeader1Link,"+","-");
  285.                         toggleTitle(customTagsHeader1Link,"Show custom tags","Hide custom tags");
  286.                         toggleTitle(customTagsHeader2Link,"Show custom tags","Hide custom tags");
  287.                     });
  288.             }
  289.         }
  290.     };
  291.     if (document.readyState==="loading") {
  292.         if (window.addEventListener) window.addEventListener("DOMContentLoaded",malTags,false);
  293.         else if (window.attachEvent) window.attachEvent("onload",malTags);
  294.     } else if (document.readyState==="complete") {
  295.         malTags();
  296.     } else {
  297.         if (window.addEventListener) window.addEventListener("load",malTags,false);
  298.         else if (window.attachEvent) window.attachEvent("onload",malTags);
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement