代码之家  ›  专栏  ›  技术社区  ›  Jauder Ho

DOM manip和GreaseMonkey的一些帮助

  •  2
  • Jauder Ho  · 技术社区  · 17 年前

    我不是一个JS人,所以我有点在黑暗中跌跌撞撞。基本上,我想要一个能在推特上添加一个链接的东西,用于在特定用户的页面上搜索@回复。

    我试图弄清楚两件事:

    1. 如何从页面中提取用户名,以便我可以构造正确的URL。例如,如果我在 http://twitter.com/ev ,我应该把“ev”找回来。
    2. 如何操纵DOM在正确的位置插入内容

    以下是我所针对的HTML片段:

    <ul id="tabMenu">
      <li>
        <a href="/ev" id="updates_tab">Updates</a>  </li>
      <li>
        <a href="/ev/favourites" id="favorites_tab">Favorites</a>  </li>
      </ul>
    

    以下是剧本(到目前为止):

    // ==UserScript==
    // @name         Twitter Replies Search
    // @namespace    http://jauderho.com/
    // @description  Find all the replies for a particular user
    // @include      http://twitter.com/*
    // @include      https://twitter.com/*
    // @exclude      http://twitter.com/home
    // @exclude      https://twitter.com/home
    // @author       Jauder Ho
    // ==/UserScript==
    
    var menuNode = document.getElementById('tabMenu');
    if (typeof(menuNode) != "undefined" && menuNode != null)
    {
        var html = [];
        html[html.length] = '<li>';
        html[html.length] = '<a href="http://search.twitter.com/search?q=to:ev" class="section-links" id="replies_search_tab">@Replies Search</a>';
        html[html.length] = '</li>';
    
        // this is obviously wrong
            var div = document.createElement('div');
        div.className = 'section last';
        div.innerHTML = html.join('');
        followingNode = menuNode.parentNode;
        followingNode.parentNode.insertBefore(div, followingNode);
    }
    
    3 回复  |  直到 17 年前
        1
  •  2
  •   Athena    17 年前

    这是上面的一个纯DOM方法——为了好玩,我也尝试了提取用户名:

    var menuNode = document.getElementById('tabMenu');
    if (menuNode!=null)
    {
        // extract username from URL; matches /ev and /ev/favourites
        var username = document.location.pathname.split("/")[1];
    
        // create the link
        var link = document.createElement('a');
        link.setAttribute('href', 'http://search.twitter.com/search?q=to:'+username);
        link.setAttribute('id', 'replies_search_tab');
        link.appendChild(document.createTextNode('@Replies Search'));
    
        // create the list element
        var li = document.createElement('li');
    
        // add link to the proper location
        li.appendChild(link);
        menuNode.appendChild(li);    
    }
    

    这相当于(基于原始代码片段):

      <ul id="tabMenu">
      <li>
        <a href="/ev" id="updates_tab">Updates</a>  </li>
      <li>
        <a href="/ev/favourites" id="favorites_tab">Favorites</a>  </li>
      <li>
        <a href="http://search.twitter.com/search?q=to:ev" id="replies_search_tab">@Replies Search</a></li>
      </ul>
    

    如果你想让添加的链接显示在其他位置,你需要四处走动 insertBefore 一点。

    PS.随意忽略了“section-links”类,因为这是x follow、y followers、z updates链接的格式。

        2
  •  1
  •   PhiLho    17 年前

    这是一种方法,没有经过真正的测试(没有推特帐户)。

    var userName = window.location.href.match(/^http:\/\/twitter\.com\/(\w+)/)
    if (userName == null)
      return; // Problem?
    userName = userName[1];
    var menuNode = document.getElementById('tabMenu');
    if (menuNode != null)
    {
      var html = '<a href="http://search.twitter.com/search?q=to:' +
          userName +
          '" class="section-links" id="replies_search_tab">@Replies Search</a>';
    
      var li = document.createElement('li');
      li.className = 'section last';
      li.innerHTML = html;
      menuNode.appendChild(li);
    }
    

    它将li添加到末尾,我删除了div,因为我怀疑在div中添加li是正确的。如果你真的需要将li放在ul的开头,试试 menuNode.insertBefore(li, menuNode.firstChild)

        3
  •  1
  •   Daniel X Moore mcdon    17 年前

    在没有库的情况下编写JavaScript是我永远无法回到的地狱。

    以下是一个支持jQuery的脚本框架:

    // ==UserScript==
    // @name           MyScript
    // @namespace      http://example.com
    // @description    Example
    // @include        *
    //
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
    // ==/UserScript==
    
    var menuNode = $('#tabMenu');
    if (menuNode!=null)
    {
        // extract username from URL; matches /ev and /ev/favourites
        var username = document.location.pathname.split("/")[1];
    
        // create the link
        var link = $('<a id="replies_search_tab">@Replies Search</a>');
        link.href = 'http://search.twitter.com/search?q=to:'+username;
    
        // create the list element
        var li = $('<li />');
    
        // add link to the proper location
        li.append(link);
        menuNode.append(li);    
    }
    
    推荐文章