// ==UserScript==
// @name caicicaici
// @namespace http://tampermonkey.net/
// @version 0.92
// @description caici
// @author ppppp tttt
// @require http://libs.baidu.com/jquery/2.0.0/jquery.js
// @require https://cdn.staticfile.org/html2canvas/0.5.0-beta4/html2canvas.js
// @match https://mp.weixin.qq.com/*
// @grant none
// @downloadURL https://update.greasyfork.icu/scripts/395194/caicicaici.user.js
// @updateURL https://update.greasyfork.icu/scripts/395194/caicicaici.meta.js
// ==/UserScript==
(function() {
'use strict';
var caici = `
下载
`;
var mini_cycle = ``;
var css = `
`;
if(! $('#mini_cycle').length){
$('body').append(css).append(mini_cycle).append(caici);
}
$('#mini_cycle').off('click').click(function(){
$('#caici').toggle();
});
var imgs = [];
$('.rich_pages').each(function(i,v){
var src = $(this).data('src');
imgs.push(src);
// console.log(src);
});
var title = $('.rich_media_title').text().trim();
if('undefined' != typeof(title)){
var is_caici = title.indexOf('拆词') > 0;
var date = title.substring(1,9);
// console.log(is_caici);
}
var words = [];
$('td').each(function(i,v){
var text = $(this).text();
if(typeof text !== 'undefined'){
var t = text.split('.').pop().trim();
words.push(t);
}
});
var item1 = {};
item1.title = title;
item1.date = date;
item1.words = words;
item1.imgs = imgs;
// var cg = {};
// cg['cg'+date] = item1;
var tmp = window.localStorage.__cg;
var obj1 = {};
if(typeof tmp !== 'undefined'){
obj1 = JSON.parse(tmp);
}
// if( typeof(obj['cg'+date]) === 'undefined'){
obj1['cg'+date] = item1;
window.localStorage.__cg = JSON.stringify(obj1);
// console.log(obj1);
// }
var caici_1 = {};
var caici_2 = {};
var item_obj = item1;
var all_words = words;
var img1_w = 0;
var img1_h = 0;
var img2_w = 0;
var img2_h = 0;
var cg = window.localStorage.__cg;
var obj = {};
if('undefined' !== typeof cg){
obj = JSON.parse(cg);
var str = '';
for(var i in obj){
if(obj[i].date != ''){
str += ''+obj[i].date+'';
}
}
$('#list').html(str);
// console.log(obj);
}
$('#list').on('click','li',function(){
$(this).attr('class');
item_obj = obj[i];
all_words = item_obj.words;
});
// function update(i){
// item_obj = obj[i];
// all_words = item_obj.words;
// }
$('#list_cycle').click(function(){
$('#list').toggle();
});
window.element = $("#caici_jieguo"); // global variable
window.getCanvas; // global variable
$('.show_caici').click(function(){
if($('#caici_jieguo').length==0){
$('#meta_content').after('');
}
var html = $('.rich_media_title').text().trim() + "";
for(var i in all_words){
var str1 = typeof(caici_1[i]) == 'undefined' ? '' : caici_1[i];
var str2 = typeof(caici_2[i]) == 'undefined' ? '' : caici_2[i];
if(str1 == str2){
str2 = '';
}
html += ''+i+'. ' + all_words[i]+' | '+str1+' | '+str2+' |
';
}
html += '
';
$('#caici_jieguo').html(html);
html2canvas($('#caici_jieguo'), {
onrendered: function (canvas) {
$("#previewImage").append(canvas);
var imgageData = canvas.toDataURL("image/png");
var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
$("#down_img").attr("download", $('.rich_media_title').text().trim()+".png").attr("href", newData);
}
});
});
$('.show_btn,.pre_btn').off('click').click(function(){
var i = $('.id').text();
var an = $('#an').hasClass('hide');
var pre = $(this).hasClass('pre_btn');
if(an){
caici_1[i] = $('.cai').text();
$('#an').removeClass('hide');
}else{
caici_2[i] = $('.cai').text();
if(pre){
i--;
if(i<0){
i=all_words.length;
}
}else{
i++;
if(i>=all_words.length){
i=0;
}
}
$('.id').text(i);
var item = all_words[i];
var img1 = item_obj.imgs[0];
var img2 = item_obj.imgs[1];
$('#an').addClass('hide');
$('.cg1').html(item);
$('.cg2').html('
');
$('.cg3').html('
');
$('.cg4').html('');
addToCai(item);
$('.an_img1').one('load',function(){
var i = Number($('.id').text());
img1_w = $('.an_img1')[0].naturalWidth;
img1_h = $('.an_img1')[0].naturalHeight;
var div_w = $('.cg1').width();
var img1_nw = img1_w * (div_w / img1_w);
var img1_nh = img1_h * (div_w / img1_w);
$('.cg2').height(Math.floor(img1_nh/51));
var t = Math.floor(img1_nh/51*(1+i));
$('.an_img1').attr('style','width:100%;position: relative;top: -'+t+'px');
//$('.an_img1').attr('style="width:100%,top, -'+t+'"');
console.log('img1_w:'+img1_w + 'img1_h:'+img1_h);
}).attr('src', img1);
$('.an_img2').one('load',function(){
var i = Number($('.id').text());
img2_w = $('.an_img2')[0].naturalWidth;
img2_h = $('.an_img2')[0].naturalHeight;
var div_w = $('.cg1').width();
var img2_nw = img2_w * (div_w / img2_w);
var img2_nh = img1_h * (div_w / img2_w);
$('.cg3').height(Math.floor(img2_nh/51));
var t = Math.floor(img2_nh/51*(1+i));
$('.an_img2').attr('style','width:100%;position: relative;top: -'+t+'px');
//$('.an_img1').attr('style="width:100%,top, -'+t+'"');
console.log('img1_w:'+img1_w + 'img1_h:'+img1_h);
}).attr('src', img2);
// $('.an_img2').one('load',function(){
// img2_w = $('.an_img2')[0].naturalWidth;
// img2_h = $('.an_img2')[0].naturalHeight;
// var div_w = $('.cg1').width();
// img2_w = img2_w * (div_w / img2_w);
// img2_h = img2_h * (div_w / img2_w);
// $('.an_img2').height(img2_h/51*(i+2));
// console.log('img1_w:'+img2_w + 'img1_h:'+img2_h);
// }).attr('src', img2);
// $('.cg2').html(item.cg2);
// $('.cg3').html(item.cg3);
// $('.cg4').html(item.cg4);
$('.id').html(i);
}
});
$('.cai').on('click', '.space', function(){
if($(this).hasClass('black_back')){
$(this).text('').removeClass('black_back');
}else{
$(this).text(' ').addClass('black_back');
}
});
function addToCai(str){
var arr = str.split('');
var tmp_str = arr.join('');
$('.cai').html(tmp_str);
}
/*
function next(pre){
var i = $('.id').text();
var an = $('#an').hasClass('hide');
if(an){
$('#an').removeClass('hide');
}else{
if(pre){
i--;
if(i<0){
i=all_words.length;
}
}else{
i++;
if(i>=all_words.length){
i=0;
}
}
$('.id').text(i);
var item = all_words[i];
var img1 = item_obj.imgs[0];
var img2 = item_obj.imgs[1];
$('#an').addClass('hide');
$('.cg1').html(item);
$('.cg2').html('
');
$('.cg3').html('
');
// $('.cg2').html(item.cg2);
// $('.cg3').html(item.cg3);
// $('.cg4').html(item.cg4);
$('.id').html(item.i);
}
}
*/
//获取随机数数组 startcount为起始值 ,maxcount为最大值
function randomArr(startcount, maxcount) {
var arr = [];
for (var i = startcount; i < maxcount; i++) {
arr.push(i);
}
return arr;
}
//取出随机数 arr为数组,maxNum为取出随机数的个数
function RandomNumBoth(arr, maxNum) {
var numArr = [];
var arrLength = arr.length;
for (var i = 0; i < arrLength; i++) {
var Rand = arr.length;
//取出随机数
var number = Math.floor(Math.random() * arr.length); //生成随机数num
numArr.push(arr[number]); //往新建的数组里面传入数值
arr.splice(number, 1); //传入一个删除一个,避免重复
if (arr.length <= arrLength - maxNum) {
return numArr;
}
}
}
var arr = randomArr(0, all_words.length);
arr = RandomNumBoth(arr, all_words.length);
})();