2011-08-15 34 views
0

我对编程非常陌生,并且通读了很多“Diving into Greasemonkey”(Mark Pilgrim)书中的示例,但我无法使其工作。将JavaScript转换为Greasemonkey脚本

我基本上需要将下面的代码转换成可以在给定网站上运行的Greasemonkey脚本。我看到这个代码发布在其他地方,它的功能是在任何页面上创建一个可切换的URL地址栏。

所以基本上你可以使用这个JavaScript URL栏而不是浏览器的URL栏。您通常通过执行http://www.site-with-script.com/?url=www.google.com来运行它(在这种情况下,它会将可触摸的URL栏放在Google页面的顶部)。

我尝试了Greasemonkey中的一些innerHTML和CSS的东西,但我无法让它工作。 JavaScript代码工作100%。我只是不知道如何让它作为Greasemonkey脚本工作。我想要的Greasemonkey脚本,把同样的地址栏的每个页面上,当脚本加载:

<head> 
     <style type="text/css"> 
      html, body { 
       margin: 0; 
       padding: 0; 
       width: 100%; 
       height: 100%; 
       min-height: 100%; 
       background: #BBB; 
       color: #222; 
       text-shadow: 0 1px 0 rgba(255,255,255,0.5); 
       -moz-text-shadow: 0 1px 0 rgba(255,255,255,0.5); 
       -webkit-text-shadow: 0 1px 0 rgba(255,255,255,0.5); 
       overflow: hidden; 
      } 

      #div_frameHolder { 
       display: block; 
       position: absolute; 
       left: 0; 
       top: 0; 
       width: 100%; 
       height: 100%; 
       background: #FFF; 
       border: none; 
       z-index: 100; 
      } 

      iframe.iframe_tab { 
       display: block; 
       position: absolute; 
       left: 0; 
       top: 0; 
       width: 100%; 
       height: 100%; 
       background: #FFF; 
       border: none; 
       z-index: 100; 
      } 

      .toolbar { 
       display: none; 
       position: absolute; 
       left: 0; 
       top: 0; 
       width: 100%; 
       height: 22px; 
       background: rgba(0,0,0,0.8); 
       border: none; 
       border-bottom: 1px solid rgb(0,0,0); 
       box-shadow: 0 -3px 10px #000; 
       -moz-box-shadow: 0 -3px 10px #000; 
       -webkit-box-shadow: 0 -3px 10px #000; 
       overflow: hidden; 
       z-index: 600; 
      } 

      .toolbar .bottom { 
       position: absolute; 
       left: 0; 
       bottom: 0; 
       width: 100%; 
       height: 1px; 
       background: #777; 
       border: none; 
       overflow: hidden; 
       font-size: 1px; 
      } 

      #btn_showToolbar, #btn_hideToolbar { 
       position: absolute; 
       left: 0; 
       top: 0; 
       display: block; 
       width: 22px; 
       height: 22px; 
       background: url('img/show-toolbar.png') center no-repeat; 
       border: none; 
       cursor: pointer; 
       z-index: 500; 
      } 
      body #btn_hideToolbar { 
       position:relative; 
       float: left; 
       clear: none; 
      } 

      #text_locationBar { 
       position: relative; 
       display: block; 
       width: 400px; 
       height: 18px; 
       float: left; 
       clear: none; 
       padding: 0; 
       margin: 1px 2px 0; 
       background: rgba(150,150,150,0.8); 
       border: 1px solid rgba(255,255,255,0.5); 
       color: #FFF; 
       text-shadow: 0 -1px 0 rgba(0,0,0,0.5); 
       -moz-text-shadow: 0 -1px 0 rgba(0,0,0,0.5); 
       -webkit-text-shadow: 0 -1px 0 rgba(0,0,0,0.5); 
       text-indent: 1px; 
      } 

      #btn_locationGo { 
       position: relative; 
       display: block; 
       width: 22px; 
       height: 22px; 
       float: left; 
       clear: none; 
       padding: 0; 
       margin: 0 2px; 
       background: url('img/go.png') center no-repeat; 
       border: none; 
       cursor: pointer; 
       opacity: 0.7; 
       filter: alpha(opacity=70); 
      } 
      #btn_locationGo:hover { 
       opacity: 1; 
       filter: alpha(opacity=100); 
      } 
     </style> 


     <script type="text/javascript"> 
      window.urlbar = (function() { 
       var self  = {}, 
        initialized = false, 
        showing  = false, 
        tabs  = [], 
        tabMaxId = 0, 
        homeUrl  = null, 
        toolbar, 
        showBtn, 
        hideBtn, 
        locationBar, 
        locationGo, 
        frameHolder; 

       function el(id) { 
        return document.getElementById(id); 
       } 

       function vis(el, visible) { 
        el.style.display = visible===false ? "none" : "block"; 
       } 

       function addEvent(obj, type, fn) { 
        if (obj.attachEvent) { 
         obj['e'+type+fn] = fn; 
         obj[type+fn] = function(){obj['e'+type+fn](window.event);} 
         obj.attachEvent('on'+type, obj[type+fn]); 
        } else 
         obj.addEventListener(type, fn, false); 
       } 

       function removeEvent(obj, type, fn) { 
        if (obj.detachEvent) { 
         obj.detachEvent('on'+type, obj[type+fn]); 
         obj[type+fn] = null; 
        } else 
         obj.removeEventListener(type, fn, false); 
       } 

       function stopEvent(e) { 
        e = e || window.event; 
        if (e.preventDefault) { 
         e.preventDefault(); 
        } 
        if (e.cancelBubble) { 
         e.cancelBubble(); 
        } 
        e.returnValue = false; 
        return false; 
       } 

       function init() { 
        if (initialized===true) { 
         return false; 
        } 
        initialized = true; 

        toolbar = el("urlbar_toolbar"); 
        showBtn = el("btn_showToolbar"); 
        hideBtn = el("btn_hideToolbar"); 
        locationBar = el("text_locationBar"); 
        locationGo = el("btn_locationGo"); 
        frameHolder = el("div_frameHolder"); 

        addEvent(showBtn, "mousedown", stopEvent); 
        addEvent(showBtn, "click", function() { 
         self.showToolbar(); 
         return stopEvent.apply(this,arguments); 
        }); 

        addEvent(hideBtn, "mousedown", stopEvent); 
        addEvent(hideBtn, "click", function() { 
         self.hideToolbar(); 
         return stopEvent.apply(this,arguments); 
        }); 

        addEvent(locationGo, "mousedown", stopEvent); 
        addEvent(locationGo, "click", function() { 
         var didNavigate = self.navigate(locationBar.value); 
         if (didNavigate) { 
          self.hideToolbar(); 
         } 
         return stopEvent.apply(this,arguments); 
        }); 

        addEvent(locationBar, "keydown", function(e) { 
         e = e || window.event; 
         var key = e.keyCode || e.which; 
         var rval = true; 
         switch (key) { 
          case 13:  // enter 
           var didNavigate = self.navigate(locationBar.value); 
           if (didNavigate) { 
            self.hideToolbar(); 
           } 
           rval = false; 
          break; 
         } 
         if (rval===false) { 
          return stopEvent.apply(this,arguments); 
         } 
        }); 

        var getHomeUrl = window.location.href.match(/[\?&](url|location)\=(.+)$/); 
        homeUrl = (getHomeUrl && getHomeUrl[2]) || null; 

        if (homeUrl) { 
         self.navigate(homeUrl); 
        } 
        else { 
         self.showToolbar(); 
        } 

        tabs.push({ 
         navigate : function(url) { 
          this.iframe.src = url; 
         }, 
         iframe : el("iframe_tab_0") 
        }); 
       } 

       function doResize() { 
        var w = window.innerWidth || (document.documentElement||document.body).offsetWidth; 
        if (showing===true) { 
         locationBar.style.width = (w - locationGo.offsetWidth - hideBtn.offsetWidth - 20) + "px"; 
        } 
       } 

       function openTab(options) { 
        options = options || {}; 
        var iframe = document.createElement("iframe"); 
        iframe.setAttribute("frameborder", "0"); 
        iframe.className = "iframe_tab"; 
        if (options.url || options.location) { 
         iframe.setAttribute("src", options.url || options.location); 
        } 
        if (options.focus===true || options.focussed===true) { 
         iframe.style.zIndex = "200"; 
        } 

        var tab = { 
         id : tabMaxId++, 
         iframe : iframe, 
         navigate : function(url) { 
          if (url) { 
           this.iframe.src = url; 
          } 
         }, 
         stop : function() { 
          this.iframe.contentWindow.stop(); 
         }, 
         back : function() { 
          this.iframe.contentWindow.back(); 
         }, 
         forward : function() { 
          this.iframe.contentWindow.forward(); 
         } 
        }; 

        tabs.push(tab); 

        frameHolder.appendChild(iframe); 

        return tab; 
       } 


       self.navigate = function (url, options) { 
        options = options || {}; 
        url = url || options.url || options.location; 
        if (!url) { 
         return false; 
        } 

        if (url.indexOf("://")<0) { 
         url = "http://" + url; 
        } 

        var tab = Math.round(options.tab) || 0; 

        if (tabs[tab]) { 
         tabs[tab].navigate(url); 
        } 
        else { 
         openTab({ 
          url : url, 
          focus : true 
         }); 
        } 
        return true; 
       }; 

       self.showToolbar = function() { 
        vis(toolbar, true); 
        vis(showBtn, false); 
        showing = true; 
        doResize(); 
       }; 

       self.hideToolbar = function() { 
        vis(toolbar, false); 
        vis(showBtn, true); 
        showing = false; 
       }; 

       addEvent(window, "load", function() { 
        init(); 
       }); 

       addEvent(document, "load", function() { 
        init(); 
       }); 

       addEvent(window, "resize", function() { 
        doResize(); 
       }); 

       return self; 
      }()); 
     </script> 
    </head> 
    <body> 
     <div id="urlbar_toolbar" class="toolbar"> 
      <a id="btn_hideToolbar" class="toolbar_closebutton" href="#hide-tooblar" title="Hide Toolbar"></a> 

      <input id="text_locationBar" type="text" value="" /> 
      <a id="btn_locationGo" href="#go" title="Go to the location in the address bar"></a> 
     </div> 
     <a id="btn_showToolbar" class="floating_button" href="#show-toolbar" title="Show Toolbar"></a> 
     <div id="div_frameHolder"></div> 
     <!--<iframe id="iframe_tab_0" class="webview" src="" frameborder="0"></iframe>--> 
    </body> 
</html> 

回答

0

唯一的事情,使区别,如果比较标准的JavaScript和Greasemonkey的一个,是原点,或回调函数。

标准的JavaScript代码来源是当前的浏览器窗口,但Greasemonkey脚本在“另一个维度”中工作。要在Greasemonkey脚本中使用浏览器窗口,您需要使用回调,在Greasemonkey中称为unsafeWindow。因此,如果您想使用在浏览器中加载的jQuery代码,请致电unsafeWindow.jquery.stuff()

你可以在their wiki page了解更多关于它的信息。

+0

啊哈!谢谢!我实际上很接近现在开始工作=) –