ホームページ  >  記事  >  ウェブフロントエンド  >  javascript SocialHistory 訪問者が特定の site_javascript スキルを訪問したかどうかを確認します

javascript SocialHistory 訪問者が特定の site_javascript スキルを訪問したかどうかを確認します

WBOY
WBOYオリジナル
2016-05-16 19:02:05855ブラウズ
に表示するソーシャル ブックマーク バッジを認識できるようにします。 を訪問しました
window.onload = function() {
var sl = new SocialHistory();
warning(sl.doesVisit("www.jb51.net"));
ユーザーが
www.jb51.net を使用したことがある場合、この関数は true を返し、それ以外の場合は false を返します。
実際、原理は複雑ではありません。リンクの a:visited 疑似クラスの属性を使用します。まずページ上に iframe を生成し、この iframe で a と a:visited を異なるスタイルに設定します。次に、Web サイトへのリンクを iframe に挿入します。ブラウザは、ユーザーのアクセス履歴に基づいて、訪問済みリンクの a:visited スタイルを設定します。最後に、リンクの最終的なスタイルが取得されます。それが a:visited であれば、ユーザーは Web サイトを訪問したと見なされます。具体的な実装方法についてはソースコードを参照してください。

このスクリプトは主にソーシャル ブックマークのアイコンを表示するために使用され、ユーザーが使用する Web サイトを適切に表示できます。しかし、このアプローチがユーザーのプライバシーを盗むのではないかと心配しています。この方法では、ユーザーが特定のWebサイトにアクセスしたかどうかを判定することしかできませんが、すべてのアクセス履歴を無制限に取得できるわけではありません。



/*
* ソーシャル制限 - 関心のあるソーシャルのみ。
*
* サイトが
* 訪問者。ユーザーがアクセスしたすべてのソーシャル サイトがわかります。
* 特定のものに対するクエリ。
*
* 例:
*
* var sl = SocialHistory();
*alert( sl.doesVisit("Digg") ); // true/false を返します。不明な場合は -1 を返します。
* var listOfVisitedSites = sl.visitedSites();
* var selectedSites = sl.checkedSites();
*
* チェックするサイトをさらに追加したい場合は、それを辞書として渡すことができます
* 履歴へ:
*
* var more = { "Humanized": "http://humanized.com",
* "Azarask.in": ["http://azarask.in", "http://azarask.in/blog"]
* };
* var sl = SocialHistory(more);
* alert( sl.doesVisit("Humanized") );
*
* For a list of built-in sites, see the sites variable below.
*
* Copyright (c) 2008 Aza Raskin (http://azarask.in/blog)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

var SocialHistory = function( moreSites ){

var sites = {
"Digg": ["http://digg.com", "http://digg.com/login"],
"Reddit": ["http://reddit.com", "http://reddit.com/new/", "http://reddit.com/controversial/", "http://reddit.com/top/", "http://reddit.com/r/reddit.com/", "http://reddit.com/r/programming/"],
"StumbleUpon": ["http://stumbleupon.com"],
"Yahoo Buzz": ["http://buzz.yahoo.com"],
"Facebook": ["http://facebook.com/home.php", "http://facebook.com", "https://login.facebook.com/login.php"],
"Del.icio.us": ["https://secure.del.icio.us/login", "http://del.icio.us/"],
"MySpace": ["http://www.myspace.com/"],
"Technorati": ["http://www.technorati.com"],
"Newsvine": ["https://www.newsvine.com", "https://www.newsvine.com/_tools/user/login"],
"Songza": ["http://songza.com"],
"Slashdot": ["http://slashdot.org/"],
"Ma.gnolia": ["http://ma.gnolia.com/"],
"Blinklist": ["http://www.blinklist.com"],
"Furl": ["http://furl.net", "http://furl.net/members/login"],
"Mister Wong": ["http://www.mister-wong.com"],
"Current": ["http://current.com", "http://current.com/login.html"],
"Menaeme": ["http://meneame.net", "http://meneame.net/login.php"],
"Oknotizie": ["http://oknotizie.alice.it", "http://oknotizie.alice.it/login.html.php"],
"Diigo": ["http://www.diigo.com/", "https://secure.diigo.com/sign-in"],
"Funp": ["http://funp.com", "http://funp.com/account/loginpage.php"],
"Blogmarks": ["http://blogmarks.net"],
"Yahoo Bookmarks": ["http://bookmarks.yahoo.com"],
"Xanga": ["http://xanga.com"],
"Blogger": ["http://blogger.com"],
"Last.fm": ["http://www.last.fm/", "https://www.last.fm/login/"],
"N4G": ["http://www.n4g.com"],
"Faves": ["http://faves.com", "http://faves.com/home", "https://secure.faves.com/signIn"],
"Simpy": ["http://www.simpy.com", "http://www.simpy.com/login"],
"Yigg": ["http://www.yigg.de"],
"Kirtsy": ["http://www.kirtsy.com", "http://www.kirtsy.com/login.php"],
"ファーク": ["http://www.fark.com", "http://cgi.fark.com/cgi/fark/users.pl?self=1"],
"Mixx": ["https://www.mixx.com/login /dual", "http://www.mixx.com"],
「Google ブックマーク」: ["http://www.google.com/ブックマーク" "http://www.google.com/ig/add?moduleurl=bookmarks.xml&hl=ja" ]、
"送信": ["http://subbmitt.com/"]
};

for( var サイト in詳細サイト ) {
// サイトがない場合は、URL リストを作成します。
if( タイプ( サイト [サイト] ) == "未定義" ) サイト[サイト] = [];

// 値が文字列の場合は、それを URL リストにプッシュするだけです。
if( typeof( moreSites [サイト] ) == "文字列" )
サイト[サイト].push( 詳細サイト[サイト] );
else
サイト[サイト] = sites[site].concat( moreSites [サイト] );
}

var= {};

function getStyle(el,scopeDoc,styleProp) {
if (el.currentStyle )
var y = el.currentStyle[styleProp];
else if (ウィンドウgetComputedStyle)
var y =scopeDoc.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
return y;
}

関数 削除( el ) {
el.parentNode.removeChild( el );
}

// コードからインスピレーションを得たもの:
// bindingzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes
function createIframe() {
var iframe = document.createElement("iframe");
iframe.style.position = "絶対";
iframe.スタイル.可視性 = "非表示";

ドキュメント.本体.appendChild(iframe);

// Firefox、Opera
if(iframe.contentDocument) iframe.doc = iframe.contentDocument ;
// Internet Explorer
else if(iframe. contentWindow) iframe.doc = iframecontentWindow.ドキュメント;

// マジック: ボディの作成を強制します (IE ではデフォルトで null)。
// 訪問済み/未訪問リンクのスタイルも強制します。
iframe.doc.open();
iframe.doc.write(');
iframe.doc.write("a{色: #000000; 表示: なし;}");
iframe.doc.write("a:visited {color: #FF0000; display:inline;}");
iframe.doc.write('');
iframe.doc.close();

// iframe を返します: iframe.doc には iframe が含まれています。
return iframe;
}

var iframe = createIframe();

function embedLinkInIframe( href, text ) {
var a = iframe.doc.createElement("a");
a.href = href;
a.innerHTML = サイト;
iframe.doc.body.appendChild( a );
}

for( var サイト inサイト ) {
var urls = サイト[サイト];
for( var i= 0; iurl.長さ; ) {
// IE の iframe のスコープ内に要素を作成する必要があります。
embedLinkInIframe( urls[i], サイト );

// 「www」を含む URL と含まない URL のバリエーションを自動的に試します
if( urls[i].一致(/www./) ){
var sansWWW = urls[i].replace(/www./, "");
embedLinkInIframe( sansWWW, サイト );
} else {
// 文字列の長さは 2 = 1、スライス オフセットは 1
var httpLen = urls[i].indexOf("//") 2;
var withWWW = urls[i].部分文字列(0, httpLen ) "www." urls[i].部分文字列( httpLen );
embedLinkInIframe( withWWW, サイト );
}

}
}

var リンク = iframe.doc.body.childNodes;
for( var i= 0; iリンク.長さ; ) {
// Firefox/Safari と IE の両方を (それぞれ) 処理します
var displayValue = getStyle(links[i]、 iframe.doc "表示");
var DidVisit = displayValue != "なし";

if( にアクセスしました){
訪問しました[ リンク[i].innerHTML ] = true;
}
}
remove( iframe );

戻る 新しい (関数( ){
var usedSites = [];
for( var サイト in ){
usedSites.push( サイト );
}

// 訪問したサイトの配列を返します。
この.訪問したサイト= 関数 () {
戻る usedSites;
}

// true/false を返します。サイトをチェックしなかった場合は、-1 を返します。
この.は、= 関数にアクセスします( サイト ) {
if( タイプ( サイト [サイト] ) == "未定義" )
return -1;
return typeof( が訪問しました[サイト] ) != "未定義" ;
}

var selectedSites = [];
for( var サイト inサイト ){
checkedSites.push( サイト );
}
// チェックしたサイトのリストを返します。
この.checkedSites = 関数 (){
returncheckedSites;
}
})();
}
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。