Skip to content

Commit

Permalink
Closes #106 using selected text from gmail's reply box when it shouldn't
Browse files Browse the repository at this point in the history
  • Loading branch information
Quicksaver committed Feb 19, 2016
1 parent f5821f5 commit 4462285
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
40 changes: 29 additions & 11 deletions resource/modules/content/fillSelectedText.jsm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// VERSION 2.0.7
// VERSION 2.0.8

this.selectedText = {
handleEvent: function(e) {
Expand Down Expand Up @@ -57,24 +57,42 @@ this.selectedText = {
// there's no point in autofilling the find bar if it won't work in this page
if(!Finder.isValid) { return; }

let selText = Finder.getActiveSelectionText();
let activeText = Finder.getActiveSelectionText();

// don't autofill if we're selecitng text in an editable node and the user doesn't want that,
// but we do want to erase the findbar when there's no text selection
if(selText && !Prefs.fillTextFromEditable) {
let focused = Finder.getFocused();
if(focused.element) {
// instances of any editable element (i.e. input,textarea) are of course editable
if(focused.element instanceof Ci.nsIDOMNSEditableElement) { return; }

// in HTML5, elements with contenteditable="true" are freely editable
if(trueAttribute(focused.element, 'contenteditable')) { return; }
if(activeText.text && !Prefs.fillTextFromEditable && activeText.focusedElement) {
// instances of any editable element (i.e. input,textarea) are of course editable
if(activeText.focusedElement instanceof Ci.nsIDOMNSEditableElement) { return; }

// in HTML5, elements with contenteditable="true" are freely editable
if(trueAttribute(activeText.focusedElement, 'contenteditable')) { return; }

// To fix a very specific case for gmail's new reply box, while trying to generalize for other possible similar cases.
// see https://github.com/Quicksaver/FindBar-Tweak/issues/106#issuecomment-186307441
if(activeText.selection && activeText.selection.rangeCount == 1) {
let range = activeText.selection.getRangeAt(0);
if(range.startContainer == range.endContainer && range.startContainer.childNodes.length) {
// This string can differ from the above. For instance when quoting text, the above string will include the ">" characters,
// while this will not as the text will be preformated into quote blocks.
let rangeText = Finder.trimText(range.toString());

let doc = range.startContainer.ownerDocument;
let editable = $$('[contenteditable="true"]', range.startContainer);
for(let child of editable) {
try{
let textContent = Finder.trimText(child.textContent);
if(textContent == rangeText) { return; }
}
catch(ex) { /* ignore */ }
}
}
}
}

this.noSights(true);

message('FillSelectedText', selText);
message('FillSelectedText', activeText.text);
}, 0);
}
};
Expand Down
28 changes: 16 additions & 12 deletions resource/modules/content/mFinder.jsm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// VERSION 1.0.21
// VERSION 1.0.22

this.__defineGetter__('isPDFJS', function() { return Finder.isPDFJS; });

Expand Down Expand Up @@ -214,7 +214,7 @@ this.Finder = {

// Forcibly set the search string of the find clipboard to the currently selected text in the window, on supported platforms (i.e. OSX).
setSearchStringToSelection: function() {
let searchString = this.getActiveSelectionText();
let searchString = this.getActiveSelectionText().text;

// Empty strings are rather useless to search for.
if(!searchString.length) {
Expand Down Expand Up @@ -269,23 +269,27 @@ this.Finder = {

getActiveSelectionText: function() {
let focused = this.getFocused();
let selText;
let selection = null;
let text = "";

if(focused.element instanceof Ci.nsIDOMNSEditableElement && focused.element.editor) {
// The user may have a selection in an input or textarea.
selText = focused.element.editor.selectionController.getSelection(Ci.nsISelectionController.SELECTION_NORMAL).toString();
selection = focused.element.editor.selectionController.getSelection(Ci.nsISelectionController.SELECTION_NORMAL);
} else {
// Look for any selected text on the actual page.
selText = focused.window.getSelection().toString();
selection = focused.window.getSelection();
}

if(!selText) {
return "";
if(selection) {
// Process our text to get rid of unwanted characters.
text = this.trimText(selection.toString());
}

// Process our text to get rid of unwanted characters.
//return selText.trim().replace(/\s+/g, " ").substr(0, 150);
return selText.trim().replace(/\s+/g, " ");
return { focusedElement: focused.element, selection, text };
},

trimText: function(str) {
return str.trim().replace(/\s+/g, " ");
},

enableSelection: function() {
Expand Down Expand Up @@ -1193,12 +1197,12 @@ this.RemoteFinderListener = {

this.addMessage("GetInitialSelection", () => {
var selection = Finder.getActiveSelectionText();
message("CurrentSelectionResult", { selection: selection, initial: true });
message("CurrentSelectionResult", { selection: selection.text, initial: true });
});

this.addMessage("GetTextSelection", () => {
var selection = Finder.getActiveSelectionText();
message("TextSelectionResult", selection);
message("TextSelectionResult", selection.text);
});

this.addMessage("FastFind", (data) => {
Expand Down

0 comments on commit 4462285

Please sign in to comment.