Hi-history

获取网站浏览历史

As of 2019-02-06 12:09:23 UTC. See the latest version.

// ==UserScript==
// @name         Hi-history
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  获取网站浏览历史
// @author       You
// @match        http://*/*
// @match        https://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    document.body.addEventListener('mousedown',function(e){
		var name=location.hostname.split('.')[1];
		var c_name=location.hostname.split('.')[0];
		console.log("okok");
		if((String(e.target).search(name)<0||String(e.target).search(c_name)<0)&&String(e.target).search("http")>=0){
			e.target.target = '_blank';}else if((String(e.target.parentNode).search(name)<0||String(e.target.parentNode).search(c_name)<0)&&String(e.target.parentNode).search("http")>=0){
				e.target.parentNode.target='_blank';}else if((String(e.target.parentNode.parentNode).search(name)<0||String(e.target.parentNode.parentNode).search(c_name)<0)&&String(e.target.parentNode.parentNode).search("http")>=0){
					e.target.parentNode.parentNode.target='_blank';}else if((String(e.target.parentNode.parentNode.parentNode).search(name)<0||String(e.target.parentNode.parentNode.parentNode).search(c_name)<0)&&String(e.target.parentNode.parentNode.parentNode).search("http")>=0){
						e.target.parentNode.parentNode.parentNode.target='_blank';}else if((String(e.target.parentNode.parentNode.parentNode.parentNode).search(name)<0||String(e.target.parentNode.parentNode.parentNode.parentNode).search(c_name)<0)&&String(e.target.parentNode.parentNode.parentNode.parentNode).search("http")>=0){
							e.target.parentNode.parentNode.parentNode.parentNode.target='_blank';}else{
                                if(String(e.target).search(name)>=0){
                                    e.target.target = '_self';}else if(String(e.target.parentNode).search(name)>=0){
                                        e.target.parentNode.target='_self';}else if(String(e.target.parentNode.parentNode).search(name)>=0){
                                            e.target.parentNode.parentNode.target='_self';}else if(String(e.target.parentNode.parentNode.parentNode).search(name)>=0){
                                                e.target.parentNode.parentNode.parentNode.target='_self';}else if(String(e.target.parentNode.parentNode.parentNode.parentNode).search(name)>=0){
                                                    e.target.parentNode.parentNode.parentNode.parentNode.target='_self';}else{}
                            }
		
	});
	//创建样式 
	var dom=document.createElement('style'),//alert
	dom_body=document.getElementsByTagName('body')[0];//.del
	dom.innerHTML='#markBook{position: fixed;left: -220px;top: 5px;z-index: 999;border-radius:5px;background-color: cornsilk;box-sizing: border-box;width: 220px;transition:all 400ms}.bookNav{ overflow: hidden;/*border-radius: 5px;*/}.del_mark{position: absolute;bottom: 0px;left:0px;}.bookNav,.del_mark{width: 100%;height: 28px;line-height: 28px;/*position: absolute;bottom: 0px;*/background-color: lightgray;font-size: 16px;color: #A9A9A9;/*border: 1px solid #000000;*/}.hismark_history{float: left;width: 70px;line-height: 28px;box-sizing: border-box;display: inline;height: 28px;text-align: center;font-size: 16px;color: #000;cursor: pointer;}.hismark_active{background-color: #BDBDBD;border-bottom: 2px solid #A9A9A9;box-sizing: border-box;}.exit{float:left;width: 150px;height: 28px;line-height: 28px;text-align:center;box-sizing: border-box;border-left: 2px solid #777777;cursor: pointer;}.exit:hover{color: red;}.notice{height: 280px;width: 190px;line-height: 40px;}.history_tab{display: block;padding: 10px;padding-bottom: 35px;}.hismark_del{float: right;width: 80px;height: 28px;line-height: 28px;font-size: 12px;text-align: center;color: #000;cursor:pointer}.hismark_del:hover{color: red;}.s_item{width:190px;height: 40px;border-bottom: 1px solid #000000;padding-left: 5px;line-height: 40px;overflow: hidden;text-overflow: ellipsis;}.s_item a{text-decoration: none;font-size: 14px;}.record_pull{position: absolute;top: 0px;left: 220px;width: 40px;height: 40px;background-color: lightgray;border-radius: 5px;line-height: 40px;font-size: 30px;color: #FFFFFF;text-align: center;cursor: pointer;transition:all 400ms}';
	dom_body.appendChild(dom);
	//创建标签
	dom=document.createElement('div');
	dom.id="markBook";
	var str="<div class='bookNav'><div class='hismark_history hismark_active'>历史</div><div class='exit'>退出</div></div>";
	hismark_init();
	//创建按钮
	str+="<div class='record_pull'>S</div>";
	dom.innerHTML=str;
	/*alert(str);
	alert(dom.innerHTML);*/
	dom_body.appendChild(dom);
	var hist=document.getElementsByClassName('hismark_history')[0];
	var history_tab=document.getElementsByClassName('history_tab')[0];
	hist.addEventListener("mouseover",function(){
		hist.className="hismark_history hismark_active";history_tab.style.display='block';});
		
	//按钮事件
	var record_pull=document.getElementsByClassName("record_pull")[0];
	var markBook=document.getElementById('markBook');
	var exit_bnt=document.getElementsByClassName('exit')[0];
	record_pull.onclick=function(){
		markBook.style.left=5+"px";record_pull.style.top=0+"px";record_pull.style.right=0+"px";record_pull.style.display="none";
		//超出网页可视高度时固定高度,tab部分超出滚动,待改进
		var W_height=document.documentElement.clientHeight;
		var scroll_height=document.documentElement.clientHeight-86;
		var object_height=document.getElementById('markBook').clientHeight;
		var scroll_tab=document.getElementsByClassName("hismark_tab");
		console.log(typeof scroll_height);
		console.log(scroll_height);
		if((object_height+40)>W_height){
			scroll_tab[0].style.height=scroll_height+"px";
			scroll_tab[0].style.overflow="scroll";
			scroll_tab[1].style.height=scroll_height+"px";
			scroll_tab[1].style.overflow="scroll";
		}
	};
	exit_bnt.onclick=function(){markBook.style.left=-220+"px";record_pull.style.top=0+"px";record_pull.style.right=-40+"px";record_pull.style.display="block";};
	
	//清空历史操作
	var hismark_del=document.getElementsByClassName("hismark_del");
	hismark_del[0].onclick=function(){
		alert(this.parentNode.parentNode.children[0].innerHTML);
		this.parentNode.parentNode.innerHTML="";
		
		//重新载入页面
		window.location.reload();
	};
	
	function hismark_init(){
		var notice="";
        if (top != window) {
	        console.log("Not a top window!");
	        return;
	    }
		//获取浏览记录
		if(!sessionStorage)
		{
			str+="<div class='notice'>浏览器不支持sessionStorage!</div>";
		}else{
            console.log('first record!');
			//var storage_length=sessionStorage.length;
			console.log('storage_length: '+his_item_length());
			if(his_item_length()<1){
				//建立第一条记录
				sessionStorage.setItem('url_1',location.href);
				sessionStorage.setItem('url_1_name',(document.getElementsByTagName('title')[0]).innerHTML);
//				alert(sessionStorage.getItem('url_1'));
				
				str+="<div class='history_tab'><div class='notice'><div class='s_item'><a href="+sessionStorage.getItem('url_1')+">1:"+sessionStorage.getItem('url_1_name')+"</a></div></div></div>";
				sessionStorage.setItem('mark_count',0);
			}else{
				console.log('continue!');
				history_tab();
			}
		}
	}
    function history_tab(){
		str+="<div class='history_tab hismark_tab'>";
		var url_name='url_'+(his_item_length()+1);
		sessionStorage.setItem(url_name,window.location.href);
		sessionStorage.setItem(url_name+'_name',(document.getElementsByTagName('title')[0]).innerHTML);
		for(var i=1;i<=his_item_length();i++){
			if(sessionStorage.getItem('url_'+i)!=null)
			{
				str+="<div class='s_item'>"+i+":<a href="+sessionStorage.getItem('url_'+i)+">"+sessionStorage.getItem('url_'+i+'_name')+"</a></div>";
			}
		}
		str+="</div>";
	}
	function his_item_length(){
		var len=0;
		for(var i=1;i<=sessionStorage.length;i++)
		{
			if(sessionStorage.getItem('url_'+i)!=null)
				len=len+1;
		}
		return len;
	}
    // Your code here...
})();