微博图片全显示

同屏显示多图微博的全部大图。

目前为 2016-09-22 提交的版本。查看 最新版本

// ==UserScript==
// @name        微博图片全显示
// @namespace   hzhbest
// @include     http://weibo.com/*
// @include     http://www.weibo.com/*
// @description    同屏显示多图微博的全部大图。
// @version     2.0
// @grant       GM_xmlhttpRequest
// @grant       unsafeWindow
// ==/UserScript==

(function() {

var topheight = 10;

	// http://weibo.com/2710065263/BmxiVDCgt?from=page_1005052710065263_profile&wvr=6&mod=weibotime&type=comment#_rnd1436436058420
	// http://weibo.com/2328516855/CnYCvixUq?type=comment#_rnd1436493435761
var regex = /weibo\.com\/\d{10}\/[a-z0-9A-Z]{9}\??/;

// window.addEventListener('load', function(){if (regex.test(document.location.href)) {setTimeout(init, 3000);}}, false);
if (regex.test(document.location.href)) init(); //console.log(regex.test(document.location.href));

function init(){  //console.log("init");
	var list_ul = getElementsByClass("list_ul", 'div')[0]; //alert(!list_ul + "1");
	var expbox = getElementsByClass("WB_expand_media_box", 'div')[0]; //alert(!expbox + "2");
	if (!list_ul && !expbox){  //未加载评论框架,等候
		setTimeout(init, 2000); //console.log("wait2a");
		return;
	} else if (!!list_ul){
		if (!expbox) {  //console.log("exit");  //没有附加媒体容器,退出
			return;
		} else if (!list_ul.getElementsByTagName('div')[0] && !getElementsByClass("tips_rederror", 'div')[0]){   //未加载评论内容框架,等候
			setTimeout(init, 1000); //console.log("wait2b");
			return;
		} else if (document.documentElement.scrollTop < (topheight+70)){   //页面未滚动,等候
			setTimeout(init, 300); //console.log("wait2c");
			return;
		} else { //console.log(document.documentElement.scrollTop);
			go();
		}
	}
}

function go(){
	var wrpbox = getElementsByClass("WB_media_wrap", 'div')[0];        //附加媒体容器
	var expbox = getElementsByClass("WB_expand_media_box", 'div')[0];  //扩展媒体容器
	var feedbox = getElementsByClass("WB_feed_handle", 'div')[0];		//评论区分解元素
	var appbox = getElementsByClass("WB_app_view", 'div')[0];			//应用容器
	var longtext = document.getElementById("plc_main").querySelectorAll('[action-type="fl_unfold"]')[0];   //可展开的按钮
	var box = wrpbox.parentNode;										//装载提取内容的容器
	var imgthumbs = wrpbox.getElementsByTagName('img');				//提取其中的图片
	var imgsrc = [], imgs = [], imgl = imgthumbs.length;
	var _limited = false;
	
	// Insert CSS
	var headID = document.getElementsByTagName("head")[0];         
	var cssNode = creaElemIn('style', headID);
	cssNode.type = 'text/css';
	cssNode.innerHTML = '.big_pic{max-width: 890px;} .big_pic_n{max-width: 560px;} .WB_frame_c {width: auto !important; max-width: 920px; min-width: 600px;} .WB_text.W_f14, div[comment_id] .WB_text, .WB_expand>.WB_text{width: 520px;} .media_box{display: none !important;} div[node-type="comment_list"] .media_box{display: block !important;} .big_pic_sc,.big_pic_nc{position: fixed; left:10px; padding: 3px; border: 1px solid white; color: white; background: rgba(133,133,133,0.6); cursor: pointer;} .big_pic_sc{top: 40%;} .big_pic_nc{top: 45%;}';
	

	var sclink = creaElemIn('div', document.body);						//直达评论链接
	sclink.className = "big_pic_sc";
	sclink.innerHTML = "直达评论";
	sclink.addEventListener("click", function(){
		document.documentElement.scrollTop = getTop(feedbox);
	}, false);
	
	if (!!appbox) {													//检测到应用容器(微博文章或视频)时退出
		box.appendChild(appbox);
		if (!!wrpbox) box.removeChild(wrpbox);
		if (!!expbox) expbox.parentNode.removeChild(expbox);
		document.documentElement.scrollTop = topheight;
		cssNode.innerHTML = '.media_box{display: none !important;} .big_pic_sc{position: fixed; left:10px; padding: 3px; border: 1px solid white; color: white; background: rgba(133,133,133,0.6); cursor: pointer;} .big_pic_sc{top: 40%;}';
		return;
	}
	
	if (!!longtext) {													//检测到超长微博可展开的按钮,自动展开	
		getlongtext(longtext);
	}
	
	var j = 0;
	for (var i = 0; i < imgl; i++) {		console.log(j,imgthumbs[i].src);							//提取大图
		if (/sinaimg.cn\/(orj|thumb)\d{3}/.test(imgthumbs[i].src)) {		// http://ww3.sinaimg.cn/thumb180/005CeWgjjw1f28f73p6imj30cs07ot96.jpg
			imgsrc[i] = imgthumbs[i].src.replace(/sinaimg.cn\/(orj|thumb)\d{3}/, "sinaimg.cn/large");
		} else {
			j += 1;  //console.log(j,imgl,j == imgl);
			continue;
		}
		if (j == imgl) return;
		imgs[i] = creaElemIn('img', box);
		creaElemIn('br', box);
		imgs[i].src = imgsrc[i];
		imgs[i].className = "big_pic";
	}
	if (j == imgl) {
		cssNode.innerHTML = '.big_pic_sc{position: fixed; left:10px; padding: 3px; border: 1px solid white; color: white; background: rgba(133,133,133,0.6); cursor: pointer;} .big_pic_sc{top: 40%;}';
		return;
	}
	// console.log(!!wrpbox + "1 " + !!expbox + "2");
	if (!!wrpbox) box.removeChild(wrpbox);
	if (!!expbox) expbox.parentNode.removeChild(expbox);
	document.documentElement.scrollTop = topheight;

	var nclink = creaElemIn('div', document.body);						//图片限宽链接
	nclink.className = "big_pic_nc";
	nclink.innerHTML = "图片限宽";
	nclink.addEventListener("click", function(){
		if (_limited) {
			for (var i = 0; i < imgl; i++) {imgs[i].className = "big_pic";}
			_limited = false;
		} else {
			for (var i = 0; i < imgl; i++) {imgs[i].className = "big_pic_n";}
			_limited = true;
		}
	}, false);
	
}

// Create an element
function creaElemIn(tagname, destin) {
	var theElem = destin.appendChild(document.createElement(tagname));
	return theElem;
}

function getElementsByClass(cName ,tagName){
	var elements = tagName ? document.getElementsByTagName(tagName) : document.getElementsByTagName('*');
	var findEles = [];
	var reg = new RegExp('^'+cName+'\\s*|\\s+'+cName+'\\s+|\\s+'+cName+'$');
	for(var i=0;i<elements.length;i++) {
		if(reg.test(elements[i].className))findEles.push(elements[i]);          
	}
	return findEles;
}

function getTop(e){
var offset=e.offsetTop;
if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
return offset;
} 

function getlongtext(longtext) {
	var mid = longtext.getAttribute('action-data').slice(4);
	GM_xmlhttpRequest({
		'method': 'GET',
		'url': 'http://' + document.domain + '/p/aj/mblog/getlongtext?ajwvr=6&mid=' + mid,
		'onload': function (_h) {
			var html = JSON.parse(_h.responseText).data.html;
			longtext.parentNode.innerHTML = html;
		}
    });
  }

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址