Advertisement
decembre

GM - Flickr Quick Comment (Update Vispillo + inculde https)

May 30th, 2014
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            Flickr Quick Comment (Update Vispillo + NEW inculde https)
  3. // @description     Adds a select box with pre written comments. Derived from Steeev Group Promotion Tool
  4. // @author          Pierre Andrews. (code base Steeev http://steeev.f2o.org/, pt translation by Perla* <http://www.flickr.com/photos/bobnperla/>)
  5. // @namespace       http://6v8.gamboni.org/
  6. // @include         http://*flickr.com/photos/*
  7.  
  8. // @include         https://*flickr.com/photos/*
  9.  
  10. // @include         http://*flickr.com/groups/*/discuss*
  11. // @include         http://*flickr.com/groups*
  12. // @include         http://*flickr.com/photos/*/editcomment*
  13. // @exclude         http://*flickr.com/groups/*/pool*
  14. // @exclude         http://*flickr.com/groups/*/admin*
  15. // @exclude         http://*flickr.com/groups/*/map*
  16. // @exclude         http://*flickr.com/groups_members*
  17. // @exclude         http://*flickr.com/groups_invite*
  18. // @version         1.1
  19. // ==/UserScript==
  20.  
  21. // ======================================================================
  22. // To add a new quickcomment, go in the menu tools->User Scripts commands->Add new quick comment and fill the fields.
  23. // The name is what will apear in the select box.
  24. // The comment is what will be added at the cursor position of the current comment field. This field will autofill with what you have selected on the page.
  25. // ======================================================================
  26.  
  27. // --------------------------------------------------------------------
  28. //
  29. // This is a Greasemonkey user script.
  30. //
  31. // To install, you need FireFox  http://www.mozilla.org/firefox and the firefox extension called Greasemonkey: http://greasemonkey.mozdev.org/
  32. // Install the Greasemonkey extension then restart Firefox and revisit this script.
  33. // Under Tools, there will be a new menu item to "Install User Script".
  34. // Accept the default configuration and install.
  35. //
  36. // To uninstall, go to Tools/Manage User Scripts,
  37. // select "Flickr Group Promotion Tool", and click Uninstall.
  38. //
  39. // --------------------------------------------------------------------
  40.  
  41. (function() {
  42.  
  43.     var quicktags = new Array();
  44.  
  45.         /***********************************************************************
  46.      * Flickr Localisation
  47.      **********************************************************************/
  48.  
  49.     function $x1(xpath) {
  50.         return document.evaluate(
  51.                                  xpath,
  52.                                  document,
  53.                                  null,
  54.                                  XPathResult.FIRST_ORDERED_NODE_TYPE, null
  55.                                  ).singleNodeValue;
  56.     }
  57.  
  58.     var FlickrLocaliser = function(locals) {
  59.         this.init(locals);
  60.     }
  61.     FlickrLocaliser.prototype = {
  62.         selectedLang: undefined,
  63.         localisations: undefined,
  64.         getLanguage: function() {
  65.             if(!this.selectedLang) {
  66.                 var langA = $x1("//div[@id='foot-lang']//a[contains(@class,'selected')]");
  67.                 if(!langA) {
  68.                     langA= $x1("//p[@id='LanguageSelector']//a[contains(@class,'selected')]");
  69.                 }
  70.                 if(langA) {
  71.                     var matches = /\/change_language.gne\?lang=([^&]+)&.*/.exec(langA.href);
  72.                     if(matches && matches[1]) {
  73.                         this.selectedLang = matches[1];
  74.                         return this.selectedLang;
  75.                     }
  76.                 }
  77.                 return false;
  78.             } else return this.selectedLang;
  79.         },
  80.  
  81.         init: function(locals) {
  82.             this.localisations = locals;
  83.         },
  84.  
  85.         localise: function(string, params) {
  86.             if(this.localisations && this.getLanguage()) {
  87.                 var currentLang = this.localisations[this.selectedLang];
  88.                 if(!currentLang) currentLang = this.localisations[this.localisations.defaultLang];
  89.                 var local = currentLang[string];
  90.                 if(!local) {
  91.                     local = this.localisations[this.localisations.defaultLang][string];
  92.                 }
  93.                 if(!local) return string;
  94.                 for(arg in params) {
  95.                     var rep = new RegExp('@'+arg+'@','g');
  96.                     local = local.replace(rep,params[arg]);
  97.                 }
  98.                 local =local.replace(/@[^@]+@/g,'');
  99.                 return local;
  100.             } else return undefined;
  101.         }
  102.  
  103.     }
  104.  
  105.     /*****************************Flickr Localisation**********************/
  106.  
  107.  
  108.     var localiser = new FlickrLocaliser({
  109.             'en-us' :{
  110.                 'Name':'Name',
  111.                 'choose_name': 'Choose a name',
  112.                 'Comment': 'Comment',
  113.                 'Add':'Add',
  114.                 'Cancel':'Cancel',
  115.                 'prompt_comment': "Add a new quick comment",
  116.                 'Save':'Save',
  117.                 'edit_comments': "Edit quick comments",
  118.                 'quick_comments' : 'Quick Comment',
  119.                 'add_title' : 'Add A New Quick Comment',
  120.                 'edit_title' : 'Edit saved quick comments (uncheck to remove)'
  121.             },
  122.             'fr-fr': {
  123.                 // A
  124.                 'Add' : 'Ajouter',
  125.                 // C
  126.                 'Cancel' : 'Annuler',
  127.                 'Comment' : 'Commentaire',
  128.                 // N
  129.                 'Name' : 'Nom',
  130.                 // S
  131.                 'Save' : 'Sauver',
  132.                 // C
  133.                 'choose_name' : 'Choisir un nom',
  134.                 // E
  135.                 'edit_comments' : 'Editer la liste de commentaires',
  136.                 // P
  137.                 'prompt_comment' : 'Ajouter un nouveau commentaire',
  138.                 'quick_comments' : 'Commentaires Faciles',
  139.                 'add_title' : 'Ajouter Un Nouveau Commentaire Facile',
  140.                 'edit_title' : 'Editer les commentaires faciles existants (d&eacute;cocher pour enlever)'
  141.             },
  142.             'pt-br': {
  143.                 // A
  144.                 'Add' : 'Adicionar',
  145.                 // C
  146.                 'Cancel' : 'Cancelar',
  147.                 'Comment' : 'Coment&aacute;rio',
  148.                 // N
  149.                 'Name' : 'Nome',
  150.                 // S
  151.                 'Save' : 'Salvar',
  152.                 // A
  153.                 'add_title' : 'Adicionar coment&aacute;rio r&aacute;pido',
  154.                 // C
  155.                 'choose_name' : 'Escolher um nome',
  156.                 // E
  157.                 'edit_comments' : 'Editar coment&aacute;rio r&aacute;pido',
  158.                 'edit_title' : 'Editar coment&aacute;rio r&aacute;pido salvo (desmarcar para remover)',
  159.                 // P
  160.                 'prompt_comment' : 'Adicionar um novo coment&aacute;rio r&aacute;pido',
  161.                 // Q
  162.                 'quick_comments' : 'Coment&aacute;rio r&aacute;pido'
  163.             },
  164.             defaultLang:'en-us'
  165.         });
  166.  
  167.     //from http://www.nabble.com/-greasemonkey-users--GM_setValue-loses-unicode-characters-t2840046.html#a7943662
  168.     function encode_utf8( s )
  169.         {
  170.             return unescape( encodeURIComponent( s ) );
  171.         }
  172.  
  173.     function decode_utf8( s )
  174.         {
  175.             return decodeURIComponent( escape( s ) );
  176.         }
  177.  
  178.     function save() {
  179.         var value = '';
  180.         for each(q in quicktags) {
  181.             value += '@#@'+q[0]+'{#}'+q[1];
  182.         }
  183.         value = value.substr(3);
  184.         GM_setValue('quicktag',encode_utf8(value));
  185.     }
  186.  
  187.     function load() {
  188.         var value = decode_utf8(GM_getValue('quicktag'));
  189.         if(value) {
  190.             var split = value.split('@#@');
  191.             for each(q in split) {
  192.                 var s = q.split('{#}');
  193.                 quicktags.push(new Array(s[0],s[1]));
  194.             }
  195.             quicktags.sort();
  196.         }
  197.     }
  198.  
  199.     function createAutoCommenter() {
  200.         var autocommenter="&nbsp;<form name='sfcommentform'>";
  201.  
  202.         if(quicktags.length >0) {
  203.             autocommenter+="<select id=\"sfquicktag\" name='sfquicktag' onchange='sf_gpt_addcomment();'><option value='0'>-- "+localiser.localise('quick_comments')+" --</option>";
  204.             for (i=0;i<quicktags.length;i++) {
  205.                 var label = quicktags[i][0];
  206.                 var value = quicktags[i][1];
  207.                 autocommenter += '<option value="'+i+'">' +label +'</option>';
  208.             }
  209.             autocommenter += "</select>";
  210.         }
  211.  
  212.         autocommenter += "</form>";
  213.         return autocommenter;
  214.     }
  215.  
  216.     load();
  217.     arse=document.createElement('span');
  218.     arse.setAttribute('id','arse');
  219.  
  220.     var prompt_autocomment = function() {
  221.         pingHome();
  222.         var select = unsafeWindow.getSelection();
  223.  
  224.         var back = document.body.appendChild(document.createElement('div'));
  225.         back.id="poolCleaningBack";
  226.         back.setAttribute('style',"position:absolute;background-color: black;opacity: 0.35; display: block; left: 0pt;");
  227.         back.style.width = document.body.clientWidth+'px';
  228.         back.style.height = document.body.clientHeight+'px';
  229.         back.style.top = document.body.scrollTop+'px';
  230.         var modal = document.body.appendChild(document.createElement('div'));
  231.         modal.id="poolCleaning";
  232.         modal.setAttribute('style',"position:absolute;background:white;border: 3px solid black;display: block;");
  233.         modal.style.width = (document.body.clientWidth*2/3) +'px';
  234.         modal.style.left = (document.body.clientWidth*1/6) +'px';
  235.         modal.innerHTML = '<div style="padding:12px;background-color: #EEEEEE;clear:both;font-size: 14px;">'+localiser.localise('add_title')+'</div>';
  236.         modal.style.top = document.body.scrollTop+(document.body.clientHeight/2)+'px';
  237.  
  238.         var dialog = modal.appendChild(document.createElement('div'));
  239.         dialog.setAttribute('style',"padding: 18px 16px;clear:both; width:100%;");
  240.         var content = dialog.appendChild(document.createElement('div'));
  241.         var l1 = content.appendChild(document.createElement('label'))
  242.         l1.innerHTML = localiser.localise('Name')+':';
  243.         l1.style.clear="both";
  244.         var vname = content.appendChild(document.createElement('input'));
  245.         vname.type="text";
  246.         vname.value = localiser.localise("choose_name");
  247.  
  248.         content.appendChild(document.createElement('br'));
  249.         var l2 = content.appendChild(document.createElement('label'));
  250.         l2.innerHTML = localiser.localise('Comment')+':';
  251.         content.appendChild(document.createElement('br'));
  252.         var text = content.appendChild(document.createElement('textarea'));
  253.         text.rows= 10;
  254.         text.innerHTML = select;
  255.         text.setAttribute('style','width: 90%');
  256.  
  257.         var buttons = dialog.appendChild(document.createElement('div'));
  258.         var ok = buttons.appendChild(document.createElement('button'));
  259.         ok.type ='button';
  260.         ok.className='Butt';
  261.         ok.innerHTML = localiser.localise('Add');
  262.         var cancel = buttons.appendChild(document.createElement('button'));
  263.         cancel.type ='button';
  264.         cancel.className = 'Butt';
  265.         cancel.innerHTML = localiser.localise('Cancel');
  266.  
  267.         cancel.addEventListener('click',function() {
  268.                                     document.body.removeChild(back);
  269.                                     document.body.removeChild(modal);
  270.                                 },true);
  271.  
  272.  
  273.         ok.addEventListener('click',function() {
  274.                                 quicktags.push(new Array(vname.value,text.value));
  275.                                 save();
  276.                                 arse.innerHTML = createAutoCommenter();
  277.                                 document.body.removeChild(back);
  278.                                 document.body.removeChild(modal);
  279.                             },true);
  280.  
  281.         modal.style.top = document.body.scrollTop+((document.body.clientHeight-modal.scrollHeight)/2)+'px';
  282.     };
  283.     GM_registerMenuCommand(localiser.localise("prompt_comment"), prompt_autocomment);
  284.      function pingHome() {
  285.        //check everytime this is used to make statistics. Totally anonymous. Actually handled by Flickr App Garden stats.
  286.          var call = "http://api.flickr.com/services/rest/?method=flickr.tags.getHotList&count=1&format=json&api_key=46b97996d60ce60fcb28c52dd949ab50";
  287.          var img = document.body.appendChild(document.createElement('img'));
  288.          img.src =call;
  289.          img.width="1";
  290.          img.height="1";
  291.      };
  292.  
  293.     var prompt_editautocomment = function() {
  294.       pingHome();
  295.         var select = unsafeWindow.getSelection();
  296.  
  297.         var back = document.body.appendChild(document.createElement('div'));
  298.         back.id="poolCleaningBack";
  299.         back.setAttribute('style',"position:absolute;background-color: black;opacity: 0.35; display: block; left: 0pt;");
  300.         back.style.width = document.body.clientWidth+'px';
  301.         back.style.height = document.body.clientHeight+'px';
  302.         back.style.top = document.body.scrollTop+'px';
  303.         var modal = document.body.appendChild(document.createElement('div'));
  304.         modal.id="poolCleaning";
  305.         modal.setAttribute('style',"position:absolute;background:white;border: 3px solid black;display: block;");
  306.         modal.style.width = (document.body.clientWidth*2/3) +'px';
  307.         modal.style.left = (document.body.clientWidth*1/6) +'px';
  308.         modal.innerHTML = '<div style="padding:12px;background-color: #EEEEEE;clear:both;font-size: 14px;">'+localiser.localise('edit_title')+'.</div>';
  309.         modal.style.top = document.body.scrollTop+(document.body.clientHeight/2)+'px';
  310.  
  311.         var dialog = modal.appendChild(document.createElement('div'));
  312.         dialog.setAttribute('style',"padding: 18px 16px;clear:both; width:100%;overflow:auto;");
  313.         var content = dialog.appendChild(document.createElement('div'));
  314.         var ul = document.createElement('ul');
  315.         var inputs = new Array();
  316.         for (i=0;i<quicktags.length;i++) {
  317.             var label = quicktags[i][0];
  318.             var value = quicktags[i][1];
  319.             var li = document.createElement('li');
  320.             var l1 = li.appendChild(document.createElement('label'));
  321.             l1.innerHTML = label;
  322.             var vname = document.createElement('input');
  323.             vname.type="checkbox";
  324.             vname.value = i;
  325.             vname.checked = true;
  326.             li.appendChild(vname);
  327.             ul.appendChild(li);
  328.             inputs.push(vname);
  329.         }
  330.         content.appendChild(ul);
  331.  
  332.         var buttons = dialog.appendChild(document.createElement('div'));
  333.         var ok = buttons.appendChild(document.createElement('button'));
  334.         ok.type ='button';
  335.         ok.className='Butt';
  336.         ok.innerHTML = localiser.localise('Save');
  337.         var cancel = buttons.appendChild(document.createElement('button'));
  338.         cancel.type ='button';
  339.         cancel.className = 'Butt';
  340.         cancel.innerHTML = localiser.localise('Cancel');
  341.  
  342.         cancel.addEventListener('click',function() {
  343.                                     document.body.removeChild(back);
  344.                                     document.body.removeChild(modal);
  345.                                 },true);
  346.  
  347.  
  348.         ok.addEventListener('click',function() {
  349.                 new_q = new Array();
  350.                 for each(inp in inputs) {
  351.                         if(inp.checked)
  352.                             new_q.push(quicktags[inp.value]);
  353.                     }
  354.                 quicktags = new_q;
  355.                 save();
  356.                 arse.innerHTML = createAutoCommenter();
  357.                 document.body.removeChild(back);
  358.                 document.body.removeChild(modal);
  359.             },true);
  360.  
  361.         modal.style.top = document.body.scrollTop+((document.body.clientHeight-modal.scrollHeight)/2)+'px';
  362.     };
  363.     GM_registerMenuCommand(localiser.localise('edit_comments'), prompt_editautocomment);
  364.  
  365.     var w;
  366.     if (unsafeWindow)
  367.         w=unsafeWindow;
  368.     else
  369.         w=window;
  370.  
  371.     w.sf_gpt_addcomment=function() {
  372.           pingHome();
  373.       sfquicktag = document.getElementById('sfquicktag');
  374.         thisTextArea = document.evaluate(
  375.                                          "//textarea[@name='message']",
  376.                                          document,
  377.                                          null,
  378.                                          XPathResult.FIRST_ORDERED_NODE_TYPE, null
  379.                                          ).singleNodeValue;
  380.  
  381.         if(thisTextArea) {
  382.             mesg = '';
  383.             if(sfquicktag.selectedIndex != 0)
  384.                 mesg += quicktags[sfquicktag.options[sfquicktag.selectedIndex].value][1];
  385.  
  386.             try {
  387.                 thisTextArea.value = thisTextArea.value.substr(0,thisTextArea.selectionStart)
  388.                     + mesg
  389.                     + thisTextArea.value.substr(thisTextArea.selectionStart);
  390.             } catch(e) {
  391.                 thisTextArea.value += mesg;
  392.             }
  393.         }
  394.     }
  395.  
  396.     arse.innerHTML= createAutoCommenter();
  397.     var thisLink;
  398.    
  399.     if(document.location.pathname.indexOf('photos/') >= 0) {
  400.         if(document.location.pathname.indexOf('editcomment') >= 0) {
  401.             thisLink = document.evaluate('//td[@id=\'GoodStuff\']/h3',
  402.                                          document,
  403.                                          null,
  404.                                          XPathResult.FIRST_ORDERED_NODE_TYPE,
  405.                                          null
  406.                                          ).singleNodeValue;
  407.  
  408.             if(thisLink)
  409.                 thisLink.parentNode.insertBefore(arse, thisLink.nextSibling);
  410.         } else {
  411.             var allLinks;
  412.           allLinks = document.evaluate('//div[@id="DiscussPhoto"]/h3',
  413.                                        document,
  414.                                        null,
  415.                                        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  416.                                        null);
  417.           if (allLinks.snapshotLength === 0) { // new layout?
  418.              allLinks=$x1("//div[@class='comment-content']/form/div[@class='buttons']", document);
  419.              thisLink = allLinks;
  420.              thisLink.parentNode.insertBefore(arse, thisLink);
  421.              var comment = document.getElementById('comments');
  422.              comment.addEventListener('DOMNodeInserted', reloadHandler, true);
  423.           } else {
  424.             for (var i = 0; i < allLinks.snapshotLength; i++) {
  425.                 thisLink = allLinks.snapshotItem(i);
  426.                 thisLink.parentNode.insertBefore(arse, thisLink.nextSibling);
  427.             }
  428.           }
  429.         }
  430.     } else if(document.location.pathname.indexOf('discuss/') >= 0) {
  431.         if(document.location.pathname.indexOf('edit') >= 0) {
  432.             thisLink = document.evaluate('//td[@id=\'GoodStuff\']/form/p[1]/textarea',
  433.                                          document,
  434.                                          null,
  435.                                          XPathResult.FIRST_ORDERED_NODE_TYPE,
  436.                                          null
  437.                                          ).singleNodeValue;
  438.             if(thisLink)
  439.                 thisLink.parentNode.insertBefore(arse, thisLink);
  440.         } else {
  441.             thisLink = document.evaluate(
  442.                                          '//div[@id=\'DiscussTopic\']//td/h3',
  443.                                          document,
  444.                                          null,
  445.                                          XPathResult.FIRST_ORDERED_NODE_TYPE,
  446.                                          null
  447.                                          ).singleNodeValue;
  448.             if(thisLink)
  449.                 thisLink.parentNode.insertBefore(arse, thisLink.nextSibling);
  450.         }
  451.     } else if(document.location.pathname == "/groups_newtopic.gne") {
  452.         thisLink = document.evaluate('//td[@id=\'GoodStuff\']/form/table/tbody/tr[2]/td[2]/textarea',
  453.                                      document,
  454.                                      null,
  455.                                      XPathResult.FIRST_ORDERED_NODE_TYPE,
  456.                                      null
  457.                                      ).singleNodeValue;
  458.         if(thisLink)
  459.             thisLink.parentNode.insertBefore(arse, thisLink);
  460.  
  461.     }
  462.  
  463.     function reloadHandler () {
  464.         if ((document.getElementById('arse') == null) && (document.getElementsByClassName('previewing-comment').length == 0)) {
  465.             var balls = document.createElement('span');
  466.             balls.setAttribute('id','arse');
  467.             balls.innerHTML = createAutoCommenter();
  468.             document.getElementsByClassName('buttons')[0].parentNode.insertBefore(balls,document.getElementsByClassName('buttons')[0]);
  469.         }
  470.     }  
  471.  
  472. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement