]> git.wh0rd.org Git - tt-rss.git/commitdiff
quickAddFilter: if text is currently highlighted, use that instead of active article...
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Tue, 12 Nov 2013 10:21:28 +0000 (14:21 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Tue, 12 Nov 2013 10:21:28 +0000 (14:21 +0400)
js/functions.js

index fdd79159859d8261dd67b72f8cb6b914b1805e2f..43fae715b633fb0e70db9a8cd01f10baaa13f8bf 100644 (file)
@@ -1150,33 +1150,48 @@ function quickAddFilter() {
                        href: query});
 
                if (!inPreferences()) {
+                       var selectedText = getSelectionText();
+
                        var lh = dojo.connect(dialog, "onLoad", function(){
                                dojo.disconnect(lh);
 
-                               var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
+                               if (selectedText != "") {
 
-                               new Ajax.Request("backend.php", {
-                               parameters: query,
-                               onComplete: function(transport) {
-                                       var reply = JSON.parse(transport.responseText);
+                                       var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
+                                               getActiveFeedId();
+
+                                       var rule = { reg_exp: selectedText, feed_id: feed_id, filter_type: 1 };
+
+                                       addFilterRule(null, dojo.toJson(rule));
+
+                               } else {
 
-                                       var title = false;
+                                       var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
 
-                                       if (reply && reply) title = reply.title;
+                                       new Ajax.Request("backend.php", {
+                                       parameters: query,
+                                       onComplete: function(transport) {
+                                               var reply = JSON.parse(transport.responseText);
 
-                                       if (title || getActiveFeedId() || activeFeedIsCat()) {
+                                               var title = false;
 
-                                               console.log(title + " " + getActiveFeedId());
+                                               if (reply && reply) title = reply.title;
 
-                                               var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
-                                                       getActiveFeedId();
+                                               if (title || getActiveFeedId() || activeFeedIsCat()) {
 
-                                               var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
+                                                       console.log(title + " " + getActiveFeedId());
 
-                                               addFilterRule(null, dojo.toJson(rule));
-                                       }
+                                                       var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
+                                                               getActiveFeedId();
 
-                               } });
+                                                       var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
+
+                                                       addFilterRule(null, dojo.toJson(rule));
+                                               }
+
+                                       } });
+
+                               }
 
                        });
                }
@@ -1936,3 +1951,25 @@ function feed_to_label_id(feed) {
        return _label_base_index - 1 + Math.abs(feed);
 }
 
+// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
+
+function getSelectionText() {
+       var text = "";
+
+       if (typeof window.getSelection != "undefined") {
+               var sel = window.getSelection();
+               if (sel.rangeCount) {
+                       var container = document.createElement("div");
+                       for (var i = 0, len = sel.rangeCount; i < len; ++i) {
+                               container.appendChild(sel.getRangeAt(i).cloneContents());
+                       }
+                       text = container.innerHTML;
+               }
+       } else if (typeof document.selection != "undefined") {
+               if (document.selection.type == "Text") {
+                       text = document.selection.createRange().textText;
+               }
+       }
+
+       return text.stripTags();
+}