javascript SocialHistory 检查访问者是否访问过某站点


Posted in Javascript onAugust 02, 2008
window.onload = function() {     var sl = new SocialHistory();     alert(sl.doesVisit("3water.com")); } 如果用户曾经使用过3water.com,那么该函数就会返回真,否则返回假。 其实原理并不复杂,它利用了链接的 a:visited 伪类的属性。首先在页面上生成一个iframe,并在这个iframe中设置 a 和 a:visited 为不同的样式。然后将网站的链接插入到 iframe 中。浏览器就会根据用户的访问历史,为访问过的链接设置 a:visited 的样式。最后再获得链接的最终样式,如果是 a:visited,就可以认为用户访问过该网站了。具体的实现方式可以参考源代码。 这个脚本主要用于显示社会性书签的图标,可以恰到好处地显示用户所使用的网站。但我担心,这样的做法是不是有盗取用户隐私之嫌?虽然这个方法只能判断用户有无访问特定的网站,并不能无限制地得到所有访问历史。 /*
* Social Limit - Only the social you care about.
*
* Enables your site to know which social bookmarking badges to display to your
* visitors. It tells you all social sites the user has gone to, or you can
* query for a specific one.
*
* For example:
*
* var sl = SocialHistory();
* alert( sl.doesVisit("Digg") ); // Returns true/false, -1 if unknown.
* var listOfVisitedSites = sl.visitedSites();
* var checkedSites = sl.checkedSites();
*
* If you want to add more sites to check, you can pass that in as a dictionary
* to History:
*
* 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"],
"Fark": ["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 Bookmarks": ["http://www.google.com/bookmarks", "http://www.google.com/ig/add?moduleurl=bookmarks.xml&hl=en"],
"Subbmitt": ["http://subbmitt.com/"]
};
for( var site in moreSites ) {
// If we don't have the site, create the URL list.
if( typeof( sites[site] ) == "undefined" ) sites[site] = [];
// If the value is string, just push that onto the URL list.
if( typeof( moreSites[site] ) == "string" )
sites[site].push( moreSites[site] );
else
sites[site] = sites[site].concat( moreSites[site] );
}
var visited = {};
function getStyle(el, scopeDoc,styleProp) {
if (el.currentStyle)
var y = el.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = scopeDoc.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
return y;
}
function remove( el ) {
el.parentNode.removeChild( el );
}
// Code inspired by:
// bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes
function createIframe() {
var iframe = document.createElement("iframe");
iframe.style.position = "absolute";
iframe.style.visibility = "hidden";
document.body.appendChild(iframe);
// Firefox, Opera
if(iframe.contentDocument) iframe.doc = iframe.contentDocument;
// Internet Explorer
else if(iframe.contentWindow) iframe.doc = iframe.contentWindow.document;
// Magic: Force creation of the body (which is null by default in IE).
// Also force the styles of visited/not-visted links.
iframe.doc.open();
iframe.doc.write('
iframe.doc.write("a{color: #000000; display:none;}");
iframe.doc.write("a:visited {color: #FF0000; display:inline;}");
iframe.doc.write('');
iframe.doc.close();
// Return the iframe: iframe.doc contains the iframe.
return iframe;
}
var iframe = createIframe();
function embedLinkInIframe( href, text ) {
var a = iframe.doc.createElement("a");
a.href = href;
a.innerHTML = site;
iframe.doc.body.appendChild( a );
}
for( var site in sites ) {
var urls = sites[site];
for( var i=0; i
// You have to create elements in the scope of the iframe for IE.
embedLinkInIframe( urls[i], site );
// Automatically try variations of the URLS with and without the "www"
if( urls[i].match(/www\./) ){
var sansWWW = urls[i].replace(/www\./, "");
embedLinkInIframe( sansWWW, site );
} else {
// 2 = 1 for length of string + 1 for slice offset
var httpLen = urls[i].indexOf("//") + 2;
var withWWW = urls[i].substring(0, httpLen ) + "www." + urls[i].substring( httpLen );
embedLinkInIframe( withWWW, site );
}
}
}
var links = iframe.doc.body.childNodes;
for( var i=0; i
// Handle both Firefox/Safari, and IE (respectively)
var displayValue = getStyle(links[i], iframe.doc, "display");
var didVisit = displayValue != "none";
if( didVisit ){
visited[ links[i].innerHTML ] = true;
}
}
remove( iframe );
return new (function(){
var usedSites = [];
for( var site in visited ){
usedSites.push( site );
}
// Return an array of visited sites.
this.visitedSites = function() {
return usedSites;
}
// Return true/false. If we didn't check the site, return -1.
this.doesVisit = function( site ) {
if( typeof( sites[site] ) == "undefined" )
return -1;
return typeof( visited[site] ) != "undefined";
}
var checkedSites = [];
for( var site in sites ){
checkedSites.push( site );
}
// Return a list of the sites checked.
this.checkedSites = function(){
return checkedSites;
}
})();
}
Javascript 相关文章推荐
超棒的javascript页面顶部卷动广告效果
Dec 01 Javascript
语义化 H1 标签
Jan 14 Javascript
js Math 对象的方法
Sep 01 Javascript
java、javascript实现附件下载示例
Aug 14 Javascript
Bootstrap Validator 表单验证
Jul 25 Javascript
详解JS去重及字符串奇数位小写转大写
Dec 29 Javascript
BootStrap模态框和select2合用时input无法获取焦点的解决方法
Sep 01 Javascript
本地搭建微信小程序服务器的实现方法
Oct 27 Javascript
jQuery与vue实现拖动验证码功能
Jan 30 jQuery
判断iOS、Android以及PC端的示例代码
Nov 15 Javascript
微信公众号H5之微信分享常见错误和问题(小结)
Nov 14 Javascript
Vue如何清空对象
Mar 03 Vue.js
js控制框架刷新
Aug 01 #Javascript
javascript之可拖动的iframe效果代码
Aug 01 #Javascript
javascript 单选框,多选框美化代码
Aug 01 #Javascript
javascript网页关键字高亮代码
Jul 30 #Javascript
用js生产批量批处理执行命令
Jul 28 #Javascript
javascript+xml技术实现分页浏览
Jul 27 #Javascript
用JS操作FRAME中的IFRAME及其内容的实现代码
Jul 26 #Javascript
You might like
Linux下 php5 MySQL5 Apache2 phpMyAdmin ZendOptimizer安装与配置[图文]
2008/11/18 PHP
PHP6 mysql连接方式说明
2009/02/09 PHP
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
php学习笔记 数组的常用函数
2011/06/13 PHP
PHP获取php,mysql,apche的版本信息示例代码
2014/01/16 PHP
php7 安装yar 生成docker镜像
2017/05/09 PHP
修改好的jquery滚动字幕效果实现代码
2011/06/22 Javascript
jquery图片放大功能简单实现
2013/08/01 Javascript
原生js获取宽高与jquery获取宽高的方法关系对比
2014/04/04 Javascript
JavaScript利用append添加元素报错的解决方法
2014/07/01 Javascript
jquery mobile页面跳转后样式丢失js失效的解决方法
2014/09/06 Javascript
jQuery动画出现连续触发、滞后反复执行的解决方法
2015/01/28 Javascript
JavaScript中的this引用(推荐)
2016/08/05 Javascript
ionic实现滑动的三种方式
2016/08/27 Javascript
浅谈Angular.js中使用$watch监听模型变化
2017/01/10 Javascript
vue-cli开发时,关于ajax跨域的解决方法(推荐)
2018/02/03 Javascript
解决iview打包时UglifyJs报错的问题
2018/03/07 Javascript
jQuery的ztree仿windows文件新建和拖拽功能的实现代码
2018/12/05 jQuery
JavaScript动态添加数据到表单并提交的几种方式
2019/06/26 Javascript
跨平台python异步回调机制实现和使用方法
2013/11/26 Python
从Python程序中访问Java类的简单示例
2015/04/20 Python
Python使用gensim计算文档相似性
2016/04/10 Python
深入理解python中的闭包和装饰器
2016/06/12 Python
利用python进行文件操作
2020/12/04 Python
西班牙英格列斯百货英国官网:El Corte Inglés英国
2017/10/30 全球购物
大学生学习党课思想汇报
2014/01/03 职场文书
高等教育专业自荐信范文
2014/03/26 职场文书
机械设计及其自动化专业求职信
2014/06/09 职场文书
羽毛球社团活动总结
2014/06/27 职场文书
法人代表身份证明书及授权委托书
2014/09/16 职场文书
工作粗心大意检讨书
2014/09/18 职场文书
领导班子在批评与自我批评座谈会上的发言
2014/09/28 职场文书
党员批评与自我批评发言
2014/10/02 职场文书
高校自主招生校长推荐信
2015/03/23 职场文书
党员转正介绍人意见
2015/06/03 职场文书
CSS3实现的3D隧道效果
2021/04/27 HTML / CSS