2014-01-17 92 views
-1

所以我有以下的Greasemonkey腳本:Greasemonkey不工作?

// ==UserScript== 
// @name  InstaTranslate+ 
// @namespace http://use.i.E.your.homepage/ 
// @version 1.0.0 
// @description A browser add-on to easily translate words using the Google Translate engine. 
// @include  http://* 
// @copyright 2014+, Tyler Jablonski 
// ==/UserScript== 
function getSelectedText() { 
    var text = ""; 
    if (typeof window.getSelection != "undefined") { 
     text = window.getSelection().toString(); 
    } else if (typeof document.selection != "undefined" && document.selection.type == "Text") { 
     text = document.selection.createRange().text; 
    } 
    return text; 
} 

function doSomethingWithSelectedText() { 
    var selectedText = getSelectedText(); 
    if (selectedText && event.keyCode === 81) { 
       var canDo = confirm("Would you like to translate this text?"); 
       if (canDo==true) 
       { 
       var myword=selectedText; 
       var urlSetUp="http://www.translate.google.com/#auto/en/"; 
       var urlGo=urlSetUp.concat(myword); 
       window.open(urlGo); 
       } 
    } 
    else if (event.keyCode === 81) { 
      var myword=prompt("What phrase would you like to translate?"); 
      if (myword!=null) 
      { 
      var urlSetUp="http://www.translate.google.com/#auto/en/"; 
      var urlGo=urlSetUp.concat(myword); 
      window.open(urlGo); 
      } 
    } 
} 

document.onmouseup = doSomethingWithSelectedText; 
document.onkeyup = doSomethingWithSelectedText; 

不幸的是,這是行不通的。但是,當我將代碼複製並粘貼到地址欄並以此方式運行時,它可以正常工作。爲什麼是這個,我能做些什麼來糾正它?

回答

0

function doSomethingWithSelectedText()更改爲function doSomethingWithSelectedText(event),或將@grant none添加到您的元數據塊。

相關問題