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实例教程(19) 使用HoTMetal(3)
Dec 23 Javascript
这些年、我收集的JQuery代码小结
Aug 01 Javascript
js创建子窗口并且回传值示例代码
Jul 02 Javascript
图片Slider 带左右按钮的js示例
Aug 30 Javascript
jquery ajax的success回调函数中实现按钮置灰倒计时
Nov 19 Javascript
jquery实现点击弹出层效果的简单实例
Mar 03 Javascript
JS中自定义定时器让它在某一时刻执行
Sep 02 Javascript
简介JavaScript中strike()方法的使用
Jun 08 Javascript
Angular2环境搭建具体操作步骤(推荐)
Aug 04 Javascript
js实现数组和对象的深浅拷贝
Sep 30 Javascript
Node.Js生成比特币地址代码解析
Apr 21 Javascript
Vue2.2.0+新特性整理及注意事项
Aug 22 Javascript
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
PHP安全编程之加密功能
2006/10/09 PHP
PHP 工厂模式使用方法
2010/05/18 PHP
兼容ie6浏览器的php下载文件代码分享
2014/07/14 PHP
PHP获取文件相对路径的方法
2015/02/26 PHP
PHP浮点数的一个常见问题
2016/03/10 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
2020/04/06 PHP
prototype class详解
2006/09/07 Javascript
张孝祥JavaScript学习阶段性总结(2)--(X)HTML学习
2007/02/03 Javascript
JavaScript进阶练习及简单实例分析
2016/06/03 Javascript
JS快速实现移动端拼图游戏
2016/09/05 Javascript
微信小程序 教程之wxapp视图容器 swiper
2016/10/19 Javascript
对称加密与非对称加密优缺点详解
2017/02/06 Javascript
hammer.js实现图片手势放大效果
2017/08/29 Javascript
nodejs中安装ghost出错的原因及解决方法
2017/10/23 NodeJs
第一个Vue插件从封装到发布
2017/11/22 Javascript
浅析微信扫码登录原理(小结)
2018/10/29 Javascript
微信小程序动态显示项目倒计时
2019/06/20 Javascript
layui之数据表格--与后台交互获取数据的方法
2019/09/29 Javascript
NUXT SSR初级入门笔记(小结)
2019/12/16 Javascript
分析Python中设计模式之Decorator装饰器模式的要点
2016/03/02 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
Python+MongoDB自增键值的简单实现
2016/11/04 Python
python安装oracle扩展及数据库连接方法
2017/02/21 Python
Python 继承,重写,super()调用父类方法操作示例
2019/09/29 Python
Python 3.8 新功能大揭秘【新手必学】
2020/02/05 Python
浅析pip安装第三方库及pycharm中导入第三方库的问题
2020/03/10 Python
使用keras内置的模型进行图片预测实例
2020/06/17 Python
CSS3实现红包抖动效果
2020/12/23 HTML / CSS
高级3D打印市场:Gambody
2019/12/26 全球购物
Can a struct inherit from another class? (结构体能继承类吗)
2014/07/22 面试题
什么是符号链接,什么是硬链接?符号链接与硬链接的区别是什么?
2014/01/19 面试题
平面设计专业大学生职业规划书
2014/03/12 职场文书
2015年转正工作总结范文
2015/04/02 职场文书
2015年幼儿园班主任个人工作总结
2015/10/22 职场文书
团组织关系介绍信
2019/06/24 职场文书
javascript实现计算器功能详解流程
2021/11/01 Javascript