User:Erel Segal/common.js

/* / @author - Alon Zimmerman & Yitzchak Maimon / importScript('משתמש:Yaproj6/מעקב-למידה.js'); (function {   var link;    var name = mw.storage.get('name');    console.log(name);    var user = document.getElementById('pt-userpage-2');    var userName = "Erel"; // user.children[0].text;	var isUse = localStorage.getItem(userName + ':' + 'use');	if(isUse == 0 || isUse == null)	{		link = mw.util.addPortletLink('p-tb', '#', 'מעקב למידה - הוספה');	}	if(isUse == 1)	{		link = mw.util.addPortletLink('p-tb', '#', 'מעקב למידה - הסרה');		add;	}    if(!link)     {        console.log('link error!');        return;    }    var span = link.children[0].children[0];    span.style.fontSize = "16px";    span.style.color = "blue";    link.onclick = function(event) {        //event.preventDefault;        if(localStorage.getItem(userName + ':' + 'use') == 1)        {        	// alert("add")        	span.textContent = 'מעקב למידה - הוספה';        	localStorage.setItem(userName + ':' + 'use', 0);        	remove; }       else {       	// alert("rem") span.textContent = 'מעקב למידה - הסרה'; localStorage.setItem(userName + ':' + 'use', 1); add; }   };

function add { var title = $('.firstHeading').text; var isCategory = title.includes('קטגוריה:'); if (title == null || (!isCategory && title.includes(':')) || title == 'עמוד ראשי') {           return; }       var user = document.getElementById('pt-userpage-2'); var userName = "Erel"; //user.children[0].text; /**************************************************************************************************************** /////////////////////////////////////// Category - Page ///////////////////////////////////////////////////////// if(isCategory) {           var clicked = document.getElementById('firstHeading').children; if (clicked.length == 1) {               return; }           title = title.replace('קטגוריה:', ''); var pages = localStorage.getItem(userName + ':' + title); var counter = 0; if (pages != null) {               $('#mw-pages').find('li').each(function {                    counter++;                    var a = this.children;                    var t = a[0].text;                    //////////////////// if the page sign as done add checked icon next to him ///////////////////                    if ((pages.includes(t) && !pages.includes('#')) && pages.length == t.length || pages.includes('#' + t) || pages.includes(t + '#'))                    {                        //$(this).css({'color':'green', "fontSize":30}).prepend('\u2713');                        $(this).prepend(''); }               });            }			var sub_cat_counter = 0;			var sub_cat_progress = 0;            $('#mw-subcategories').find('.CategoryTreeItem').each(function  { var a = this.children; var arr = a[2].textContent.split(' '); var sub_cat_pages; //////////////////// get the number of pages in sub-category ///////////////////////////////// if(arr.length == 4) {                   sub_cat_pages = parseInt(arr[2]); }               else {               	if(a[2].textContent.includes('קט')) {               		sub_cat_pages = 0; }               	else {               		sub_cat_pages = parseInt(arr[0].replace('(', ''));                	}                }                if(isNaN(sub_cat_pages))                {                    sub_cat_pages = 1;                }                sub_cat_counter += sub_cat_pages;                var t = a[1].text;                const sub_cat = localStorage.getItem(userName + ':' + t);                if(sub_cat != null)                 {                    var page_arr = sub_cat.split(",");                    var done_pages = page_arr.length;                    sub_cat_progress += done_pages;                    var per = (done_pages / sub_cat_pages) * 100;                    //////////////////// add small progress bar next to sub-category //////////////////////////////                    $(this).find('a')                    .append(' ' + per.toFixed(2) + '%  '); }           });            //////////////////// progress of the current category /////////////////////////            if(pages == null && sub_cat_progress == 0)/////no progress            {                $('.mw-body-header').append(' 0%      ');            }            else if(pages == null)            {                var per = (sub_cat_progress / (counter + sub_cat_counter)) * 100;                //// $('.firstHeading')                $('.mw-body-header').append(' ' + '(' + per.toFixed(2) + '%) ' + 'נקראו ' + sub_cat_progress + ' דפים מתוך ' + (counter + sub_cat_counter) + '      '); }           else {               page_arr = pages.split("#"); done_pages = page_arr.length; var per = ((done_pages + sub_cat_progress) / (counter + sub_cat_counter)) * 100; //// $('.firstHeading') $('.mw-body-header').append(' ' + '(' + per.toFixed(2) + '%) ' + 'נקראו ' + (done_pages + sub_cat_progress) + ' דפים מתוך ' + (counter + sub_cat_counter) + '     '); }       } /**************************************************************************************************************** /////////////////////////////////////// Normal - Page /////////////////////////////////////////////////////////// else {           var cat = document.getElementById('mw-normal-catlinks').children; var firstCat = cat[1].firstChild.textContent; firstCat = userName + ':' + firstCat; var pages = localStorage.getItem(firstCat); if(pages != null) {               var exist = pages.includes(title); if (exist && !pages.includes('#') && title.length == pages.length || pages.includes('#' + title) || pages.includes(title + '#')) {                   //$('.mw-parser-output') $('.mw-body-header') .append('קראתי '); $("#checkBox").prop("checked", true); }               else {                   $('.mw-body-header') .append('קראתי '); }           }            else {               $('.mw-body-header') .append('<input id="checkBox" name="done" value="myValue" type="checkbox">קראתי '); }       } /**************************************************************************************************************** /////////////////////////////////////// CheckBox - Click //////////////////////////////////////////////////////// $(document).ready(function {           $("#checkBox").click(function { var cat = document.getElementById('mw-normal-catlinks').children; const catNames = cat[1]; var children = catNames.children; if($("#checkBox").is(':checked')) //////// sign as 'done' ///////// {                   for(var i = 0 ; i < children.length ; i++) {                       var child = children[i]; var name = child.textContent; name = userName + ':' + name; var pages = localStorage.getItem(name); if(pages == null) {                           localStorage.setItem(name, title); }                       else {                           if(!pages.includes('#' + title) && pages.localeCompare(title) != 0 && !pages.includes(title + '#')) {                               pages = pages.concat('#', title); localStorage.setItem(name, pages); }                       }                    }                }                else ////////sign as 'undone' ////////// {                   for(var j = 0; j < children.length; j++) { var child = children[j]; var name = child.textContent; name = userName + ':' + name; var pages = localStorage.getItem(name); var many_pages = pages.includes(","); if(many_pages) {                           pages = pages.replace('#' + title, ''); pages = pages.replace(title + '#', ''); localStorage.setItem(name, pages); }                       else {                           localStorage.removeItem(name); }                   }                }                //location.reload; });       });    } /**************************************************************************************************************** /////////////////////////////////////// REMOVE //////////////////////////////////////////////////////// function remove{ var title = $('.firstHeading').text; var isCategory = title.includes('קטגוריה:'); if (title == null || (!isCategory && title.includes(':')) || title == 'עמוד ראשי') {           return; }       if(isCategory) {       	$('.main-progress').remove; $('.sub-progress').remove; $('.green-v').remove; }       else {       	$('#label-done').remove; }   } });