2016-12-31 4 views
0

私は自分でChrome拡張機能を作成しており、これを回避することはできません。私は毎日、同じ名前の "examplespamuser"というキーワードを常に持っている、毎日たくさんのユーザーがサインアップしています。一致する.contains()のテキスト値にURLを追加する

一致するすべての結果を下線付きでコンソールに記録できますが、URLに何かを追加して一致するユーザーのURL値を変更したいと思います。

私は、ユーザーとそのURL-Sのログを記録するために、今使っているのjQueryのビット:

$(".profile-link:contains('examplespamuser')").css("text-decoration", "underline").each(function() { 
     console.log($(this).attr('href')); 
    }); 

ユーザーは常に「examplespamuser」を含んでおり、上記の部分がうまく機能フルネームで表示されます。ユーザーにはhttp://example.com/examplespamuser123/somethingにリンクするリンクがありますが、そのURLを一致するユーザーのみに変更したいので、代わりにhttp://example.com/examplespamuser123/something/elseとなります。

注jQueryとJSの新機能ですので、私はここで正しい方向に進んでいるとは確信していません。どんな助けでも大歓迎です!

+0

ある 'html'を投稿してください生成されます。 'console.log'は動作していますか? –

答えて

0

あなたが正しく理解している場合はhrefspamuserというリンクに変更する必要があります。

と仮定すると、実際のurlのは、あなたの新しいルートとしましょうhttp://example.com/examplespamuser123/something

はちょうどこの

ような何かを行うことになります/something/else

var newRoute = "/something/else"; 
 

 
$(".profile-link:contains('examplespamuser')").css("text-decoration", "underline").each(function() { 
 
    var url = $(this).attr('href'); 
 
    console.log(url); //actual url 
 
    $(this).attr('href', modifiedUrl(url)); 
 
    var url = $(this).attr('href'); 
 
    console.log(url); //modified url 
 
}); 
 

 
function modifiedUrl(url){ 
 
    var splitted = url.split("//")[1]; 
 
    var furtherSplitted = splitted.split("/").slice(0, -1); //removing last route `/something` 
 
    var withUserName = furtherSplitted.join("/"); 
 
    return "http://" + withUserName + newRoute; 
 
}
a.profile-link{ 
 
text-decoration: underline; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a class="profile-link" href="http://example.com/examplespamuser123/something"> 
 
    examplespamuser123 
 
</a>

+0

これは驚異的に機能しました!私が書いていた2番目の機能に髪を引っ張っていた。 – cardouken

+0

また、私のURLが違っていたので(/何か/ something_elseにする必要がありました)、私は分割する必要は全くなく、単に 'return url + newRoute;'を使うことができました。再度、感謝します! – cardouken

関連する問題