Un piccolo aiuto con DOM manip e GreaseMonkey
-
21-08-2019 - |
Domanda
Io non sono un ragazzo di JS così Sono un po 'inciampare nel buio. In sostanza, volevo qualcosa che aggiungere un link ad una ricerca Twitter per @ risposte a un particolare utente, mentre a pagina di quella persona.
Due cose che sto cercando di capire:
- come estrarre il nome utente dalla pagina in modo che possa costruire il proprio URL. vale a dire. se io sono sul http://twitter.com/ev , dovrei ottenere "EV" indietro.
- come manipolare il DOM per inserire le cose al posto giusto
Ecco il frammento di codice HTML che sto targeting:
<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>
E qui è lo script (finora):
// ==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);
}
Soluzione
Ecco un metodo puro-DOM di cui sopra - e per i calci, ho giocato con l'estrazione del nome utente così:
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);
}
Ciò equivale a (basato sul frammento di codice originale):
<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>
Se si desidera che il collegamento aggiunto di presentarsi in una posizione diversa, è necessario futz giro con insertBefore
un po '.
PS. Ha preso la libertà di ignorare la classe "section-link", in quanto questo è la formattazione per x dopo, i seguaci y, z aggiorna i collegamenti.
Altri suggerimenti
Ecco un modo per farlo, in realtà non testato (senza account twitter).
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);
}
Si aggiunge la Li fino alla fine, e ho lasciato cadere il div perché dubito è giusto mettere un Li in un div. Se si ha realmente bisogno di mettere la Li all'inizio della ul, provare menuNode.insertBefore(li, menuNode.firstChild)
Scrittura JavaScript senza una biblioteca è un inferno che non potrò mai tornare.
Ecco uno scheletro script che consente 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);
}