jQuery経由ですべての訪問済みリンクを選択しようとしています。ここquestion-hyperlink
が訪問されたjQueryを使用して訪問済みリンクを選択
<div class="question-summary">
<a class="question-hyperlink">Stuff</a>
</div>
場合はHTMLですが、私はquestion-summary
を選択することでした。何か案は?ここで
jQuery経由ですべての訪問済みリンクを選択しようとしています。ここquestion-hyperlink
が訪問されたjQueryを使用して訪問済みリンクを選択
<div class="question-summary">
<a class="question-hyperlink">Stuff</a>
</div>
場合はHTMLですが、私はquestion-summary
を選択することでした。何か案は?ここで
は選択です:
$("a:visited").parent(".question-summary")
使用例:
$("a:visited").parent(".question-summary").addClass("is_visited");
私は与えられたアプローチは、セキュリティ上の理由から、ブラウザで無効にされていることに言及しなければならないと思います。訪問されたリンクをチェックすることによって訪問者の履歴を取り出すことが可能であるので、これを防止するための特定の手段がとられている。
出典:
Mozilla Foundation Blog.はChromeとFFでチェック - の両方がもはや$("a:visited")
をサポートしていません。
私は彼がのlocalStorageでページのユーザーがクリックしたリンクを保存し、クラスを追加するためにクリーンなJavaScriptコードを提案Nevyan's Blog: Mark visited links using JavaScript and localStorage
にのlocalStorageに基づいて、回避策を見つけましたが<a>
要素の親に「訪問」:
function check_visited_links() {
var visited_links = JSON.parse(localStorage.getItem('visited_links')) || [];
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var that = links[i];
that.onclick = function() {
var clicked_url = this.href;
if (visited_links.indexOf(clicked_url) == -1) {
visited_links.push(clicked_url);
localStorage.setItem('visited_links', JSON.stringify(visited_links));
}
}
if (visited_links.indexOf(that.href) !== -1) {
that.parentNode.className += ' visited';
}
}
}
訪問したアプローチよりも安全かどうかは分かりません。
は、私はまた、収集するための方法を見つけようとしてhere`ItはJavaScriptでサポートされていないコードを入力してください:訪問したノードを非表示にするには、リンクデータを訪問しました。
いくつかの参照: プライバシーと:訪問したセレクタ - CSS | MDN
あなたが気にすべてがスタイリングされている場合は、CSSを通してそれを達成することができるはずですが、画面上に表示される内容によっては、それが訪問している観察するための唯一の方法である必要があります。訪問したスタイル、それらすでに訪問したリンクを表示:
私はせずに、それらのサイトができるようにするGreasemonkeyのためのuserscriptでこの方法を実行します。ただ値:
// ==UserScript==
// @description ADD a:visited for CSS
// @include *annalscts.com*
// @include *thejns.org*
// @include *turkishneurosurgery.org.tr*
// @include *nature.com*
// @include *academic.oup.com*
// @include *sagepub.com*
// @grant GM_addStyle
// ==/UserScript==
GM_addStyle('a:visited {color:#EE5665 !important}');
のために、私はちょうどAPIのためにYouTubeでチュートリアルを見て、userscriptに
GreasemonkeyのAPIを記述しようとするGreasemonkey API
GM_setValue
GM_getValue
を使用し、ローカルにデータを収集Youtubeでこのタイトルを検索する。
書かれたチュートリアル:http://nulleffort.com/greasemonkey-api-values/
Greasemonkeyのドキュメント:私のuserscriptのhttps://wiki.greasespot.net/Greasemonkey_Manual:API
一部
//Firstly, var the ordinary variable preVisitedLinks and assigning to memory variable visitedLinks (At first the value should be undefined)
var preVisitedLinks = GM_getValue("visitedLinks");
unsafeWindow.aclick = function(tlink){
window.open(tlink, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,top=10,left=10,width=10,height=10'); // click a button added and get the link visited in my script
//If the ordinary variable preVisitedLinks is undefined (First time running the script)
if(preVisitedLinks.includes('undefined')){
GM_setValue('preVisitedLinks', '|' + tlink.replace('http://paper.pubmed.cn/',''));
}
//If the ordinary variable preVisitedLinks is not undefined, continue to add each new string collect
else{
GM_setValue('preVisitedLinks', preVisitedLinks + '|' + tlink.replace('http://paper.pubmed.cn/',''));
}
//The ordinary variable preVisitedLinks assigning to memory variable visitedLinks value. The magic is the variable name the same.
preVisitedLinks = GM_getValue("preVisitedLinks");
if(preVisitedLinks.length > 27500){
preVisitedLinks = preVisitedLinks.substr(preVisitedLinks.length - 27500);
}
//The memory variable visitedLinks value assigning to the ordinary variable preVisitedLinks value
GM_setValue('visitedLinks',preVisitedLinks);
console.info(preVisitedLinks);
};
そして、私が訪問したリンクコード
if(preVisitedLinks.includes(trs[i].querySelectorAll('li')[0].querySelector('a').href.replace('http://xxx.xxxx.com/',''))){
trs[i].remove();
}
を検出するための文字列を使用して、いくつかの場所で
このような新しい質問のようです。また、答えへのリンクではなく、あなたの答えをサポートするために、ここに関連コードとドキュメンテーションを配置したいと思うでしょう。彼らはあなたが答えているものの妥当性に影響を与えて変化する可能性があります。 – rfornal
それを指摘してくれてありがとう:)私は私の答えを訂正しました。 – Maciej