2017-08-10 10 views
0

我有一個函數「snapSelectionToWord」是應該在一個谷歌的Chrome擴展onMouseUp事件被稱爲:函數是叫早的onMouseUp事件

// Initialize ability to select and grab text from browser 
    function lwsSetUpTextGetter(callback) { 

     console.log("lwsSetUpTextGetter()"); 

     //Set the onmouseup function to lwsGetText 
     document.onmouseup = snapSelectionToWord(lwsGetText); 
     //Handling clicking outside webpage? 
     if (!document.all) document.captureEvents(Event.MOUSEUP); 


    } 

但相反的功能snapSelectionToWord被稱爲立即代替等待鼠標點擊。我在這裏做錯了什麼。下面是完整的代碼...

內容腳本(JS)

(function() { 

    // Holds text being selected in browser 
    var lwsSelectedText = ''; 

    // Adds pop-up to current webpage 
    function lwsAddContent(callback) { 

     console.log("lwsAddContent()"); 

     // Get body tag 
     var body = document.getElementsByTagName('body'); 

     // add invisible div 
     document.body.innerHTML += '<div id="myModal" class="modal"><div class="modal-content"><span class="close">&times;</span><div id="lwsSpanishDiv"><p id="lwsSpanishTitle">Spanish</p><textarea id="lwsSpanishTextArea">Hello</textarea></div><div id="lwsEnglishDiv"><p id="lwsEnglishTitle">English</p><textarea id="lwsEnglishTextArea">Hello 2</textarea></div></div></div>'; 

     callback(lwsSetUpTextGetter); 

    } 

    // Make the pop-up visible and set up close button 
    function lwsActivateContent(callback) { 

     console.log("lwsActivateContent()"); 

     var modal = document.getElementById('myModal'); 

     // Get the textarea 
     var txtarea = document.getElementById("myTxtArea"); 

     // Get the <span> element that closes the modal 
     var span = document.getElementsByClassName("close")[0]; 

     span.onclick = function() { 
      modal.style.display = "none"; 
     } 

     callback(quickTranslate); 

    } 

    // Initialize ability to select and grab text from browser 
    function lwsSetUpTextGetter(callback) { 

     console.log("lwsSetUpTextGetter()"); 

     //Set the onmouseup function to lwsGetText 
     document.onmouseup = snapSelectionToWord(lwsGetText); 
     //Handling clicking outside webpage? 
     if (!document.all) document.captureEvents(Event.MOUSEUP); 


    } 

    // Snaps selected text to encompass the full word 
    function snapSelectionToWord(callback) { 

     console.log("snapSelectionToWord()"); 

     var sel; 

     // Check for existence of window.getSelection() and that it has a 
     // modify() method. IE 9 has both selection APIs but no modify() method. 
     if (window.getSelection && (sel = window.getSelection()).modify) { 
      sel = window.getSelection(); 
      if (!sel.isCollapsed) { 

       // Detect if selection is backwards 
       var range = document.createRange(); 
       range.setStart(sel.anchorNode, sel.anchorOffset); 
       range.setEnd(sel.focusNode, sel.focusOffset); 
       var backwards = range.collapsed; 
       range.detach(); 

       // modify() works on the focus of the selection 
       var endNode = sel.focusNode, endOffset = sel.focusOffset; 
       sel.collapse(sel.anchorNode, sel.anchorOffset); 
       if (backwards) { 
        sel.modify("move", "backward", "character"); 
        sel.modify("move", "forward", "word"); 
        sel.extend(endNode, endOffset); 
        sel.modify("extend", "forward", "character"); 
        sel.modify("extend", "backward", "word"); 

       } else { 
        sel.modify("move", "forward", "character"); 
        sel.modify("move", "backward", "word"); 
        sel.extend(endNode, endOffset); 
        sel.modify("extend", "backward", "character"); 
        sel.modify("extend", "forward", "word"); 
       } 
      } 
     } else if ((sel = document.selection) && sel.type != "Control") { 
      var textRange = sel.createRange(); 
      if (textRange.text) { 
       textRange.expand("word"); 
       // Move the end back to not include the word's trailing space(s), 
       // if necessary 
       while (/\s$/.test(textRange.text)) { 
        textRange.moveEnd("character", -1); 
       } 
       textRange.select(); 
      } 
     } 

     console.log("before callback..."); 

     callback(quickTranslate); 

    } 

    //Gets selected text 
    function lwsGetText(callback) { 
     return function (e) { 

      console.log("lwsGetText()"); 

      // Get access to spanish text area 
      var spanishText = document.getElementById('lwsSpanishTextArea'); 

      //Get text 
      lwsSelectedText = (document.all) ? document.selection.createRange().text : document.getSelection(); 

      //if nothing is selected, do nothing 
      if (lwsSelectedText != '') { 

       // test: does browser grab text correctly? 
       alert(lwsSelectedText); 

       // Set spanish text area content to the selected text from browser 
       spanishText.value = lwsSelectedText; 

       // --Error Here-- 
       callback(); 

      } 
     }; 
    } 

    function quickTranslate() { 

     console.log("quickTranslate()"); 

     var url = "https://translate.yandex.net/api/v1.5/tr.json/translate", 
     keyAPI = "trnsl.1.1.20130922T110455Z.4a9208e68c61a760.f819c1db302ba637c2bea1befa4db9f784e9fbb8"; 

     var englishTextArea = document.getElementById('lwsEnglishTextArea'); 
     var spanishTextArea = document.getElementById('lwsSpanishTextArea'); 

     englishTextArea.value = 'Working...'; 

     var xhr = new XMLHttpRequest(), 
      textAPI = spanishTextArea.value, 
      langAPI = 'es-en'; 
     data = "key=" + keyAPI + "&text=" + textAPI + "&lang=" + langAPI; 
     xhr.open("POST", url, true); 
     xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     xhr.send(data); 
     xhr.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       var res = this.responseText; 
       var json = JSON.parse(res); 
       if (json.code == 200) { 
        englishTextArea.value = json.text[0]; 
       } 
       else { 
        englishTextArea.value = "Error Code: " + json.code; 
       } 
      } 
     } 

    } 


    // When document ready 
    $(document).ready(function() { 

     lwsAddContent(lwsActivateContent); 

    }); 

})(); 

控制檯看起來是這樣的,如果我嘗試一下,選擇一些不添加條目:

控制檯日誌

contentScript.js:9 lwsAddContent() 
contentScript.js:24 lwsActivateContent() 
contentScript.js:45 lwsSetUpTextGetter() 
contentScript.js:58 snapSelectionToWord() 
contentScript.js:106 before callback... 

如果有人能弄清楚我在這裏做錯了什麼,那簡直太好了!謝謝!

+0

在擴展中,對於沒有添加到DOM中以添加事件偵聽器的元素,您應該*不*使用像'document.onmouseup'這樣的'on *'屬性值。這樣做是A)不好的做法,B)與頁內腳本衝突的可能性高得多。你應該使用'document.adEventListener('mouseup',...' – Makyen

+0

目前還不清楚你是否真的試圖使用'snapSelectionToWord'作爲你的事件處理程序,或者你想使用'lwsGetText'返回的函數。 'snapSelectionToWord'不會''返回'callback(quickTranslate)'的結果,所以沒有事件處理函數會被分配。但是,它確實顯示你想讓'snapSelectionToWord'成爲事件處理程序,如果是這樣,你不需要調用它,如果你想用額外的參數調用它,那麼使用'snapSelectionToWord.bind(null,lwsGetText)'通常是一個好的方法。 – Makyen

+0

在任何情況下,函數可能是你的'mouseup'事件處理函數,檢查哪個鼠標按鈕被報告,當用戶點擊除主選擇按鈕以外的任何其他按鈕時,這會導致意外的行爲 – Makyen

回答

-1
document.onmouseup = snapSelectionToWord(lwsGetText); 

變化

document.onmouseup = snapSelectionToWord; 

你應該分配的功能,而不是函數調用。也作爲snapSelectionToWord的第一個參數,您將收到event對象。

+1

更喜歡'do cument.onmouseup = function(){snapSelectionToWord(lwsGetText); }'傳遞正確的回調snapSelectionToWord –

+0

@JaromandaX這部分工作。但回調看起來並沒有通過,因爲它完成函數snapSelectionToWord後停止。想法? – TheBlindDeveloper

+0

好的,我做了一些故障排除,發現它確實啓動了函數,但是它正在返回函數(e){}所以我只是刪除了它,我們很樂意去。謝謝你的幫助! – TheBlindDeveloper