משתמש:Erel Segal/common.js
מראה
הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.
- פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
- גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
- אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
/*
/ @author - Alon Zimmerman & Yitzchak Maimon
*/
(function () {
var link;
var name = mw.storage.get('name');
var user = document.getElementById('pt-userpage-2');
var userName = 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)
{
span.textContent = 'מעקב למידה - הוספה';
localStorage.setItem(userName + ':' + 'use', 0);
remove();
}
else
{
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 = user.children[0].text;
/****************************************************************************************************************
/////////////////////////////////////////// CATEGORY /////////////////////////////////////////////////////////
*****************************************************************************************************************/
if(isCategory)
{
var current_sub_cat = 0;
var query = 'https://he.wikipedia.org/w/api.php?action=query&format=json&cmtype=subcat&cmlimit=500&list=categorymembers&cmtitle=Category:';
query += title.replace('קטגוריה:', '');
$.ajaxSetup({async: false});
$.getJSON(query, function (data) {
q = data.query;
cm = q.categorymembers;
current_sub_cat = cm.length;
});
$.ajaxSetup({async: true});
console.log(current_sub_cat);
var clicked = document.getElementById('firstHeading').children;
var total_pages = [];
var total_done_pages = [];
if(clicked.length == 1)
{
return;
}
title = title.replace('קטגוריה:', '');
var pages = localStorage.getItem(userName + ':' + title);
$('#mw-pages').find('li').each(function () {
var a = this.children;
var t = a[0].text;
total_pages.push(t);
//////////////////// if the page sign as done add checked icon next to him ///////////////////
try
{
if ((pages.includes(t) && !pages.includes('#')) && pages.length == t.length || pages.includes('#' + t) || pages.includes(t + '#'))
{
$(this).prepend('<img class="green-v" alt="" src="//upload.wikimedia.org/wikipedia/en/thumb/f/fb/Yes_check.svg/18px-Yes_check.svg.png" decoding="async" width="18" height="18" srcset="//upload.wikimedia.org/wikipedia/en/thumb/f/fb/Yes_check.svg/27px-Yes_check.svg.png 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/f/fb/Yes_check.svg/36px-Yes_check.svg.png 2x" data-file-width="600" data-file-height="600">');
}
}
catch(error){}
});
var sub_cat_pages = new Map();
var promise = 0;
$('#mw-subcategories').find('.CategoryTreeItem').each(function () {
var a = this.children;
var sub_cat = localStorage.getItem(userName + ':' + a[1].textContent);
if(sub_cat != null)
{
var page_arr = sub_cat.split("#");
total_done_pages = total_done_pages.concat(page_arr);
}
query = 'https://he.wikipedia.org/w/api.php?action=query&format=json&cmtype=page&cmlimit=500&list=categorymembers&cmtitle=Category:';
query += a[1].textContent;
$.getJSON(query, function (data) {
q = data.query;
cm = q.categorymembers;
sub_cat_pages.set(a[1].textContent, cm.length);
for(var p in cm)
{
total_pages.push(cm[p].title);
}
})
.done(function(){
promise += 1;
if(promise == current_sub_cat)
{
add_bars(total_pages, total_done_pages, sub_cat_pages, pages);
}
});
});
if(current_sub_cat == 0){add_bars(total_pages, total_done_pages, sub_cat_pages, pages);}
}
/****************************************************************************************************************
///////////////////////////////////////////// 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('<label id="label-done"><input id="checkBox" name="done" value="myValue" type="checkbox">קראתי </label>');
$("#checkBox").prop("checked", true);
}
else
{
$('.mw-body-header')
.append('<label id="label-done"><input id="checkBox" name="done" value="myValue" type="checkbox">קראתי </label>');
}
}
else
{
$('.mw-body-header')
.append('<label id="label-done"><input id="checkBox" name="done" value="myValue" type="checkbox">קראתי </label>');
}
}
/****************************************************************************************************************
//////////////////////////////////// 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);
}
}
}
});
});
}
/****************************************************************************************************************
///////////////////////////////////////////// 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();
}
}
/****************************************************************************************************************
////////////////////////////////////////// ADD - BARS /////////////////////////////////////////////////////
*****************************************************************************************************************/
function add_bars(total_pages, total_done_pages, sub_cat_pages, pages) {
var t_p = new Set(total_pages);
try
{
var page_arr = pages.split("#");
total_done_pages = total_done_pages.concat(page_arr);
}
catch(error){}
var t_d_p = new Set(total_done_pages);
var per = (t_d_p.size / t_p.size) * 100;
$('.mw-body-header').append('<div class="main-progress" style="height=8; width:60%; margin:2px; text-align:center;"><p><span style="font-size: 100%;"><b>' + '(' + per.toFixed(2) + '%) ' + 'נקראו ' + t_d_p.size + ' דפים מתוך ' + t_p.size + '</b></span></p><p style="border:1px solid #c8ccd1; padding:1px; overflow:hidden;"><span style="width: ' + (per) + '%; height: 12px; background:#00af32; float:right;"> </span><span style="width: ' + (100 - per) + '%; height: 12px; background:#f0f0f0; float:right;"> </span></p></div>');
$('#mw-subcategories').find('.CategoryTreeItem').each(function () {
var a = this.children;
var t = a[1].text;
var s_c_p = sub_cat_pages.get(t);
var sub_cat = localStorage.getItem(userName + ':' + t);
if(sub_cat != null)
{
var page_arr = sub_cat.split("#");
var done_pages = page_arr.length;
var per = (done_pages / s_c_p) * 100;
//////////////////// add small progress bar next to sub-category //////////////////////////////
$(this).find('a')
.append('<div class="sub-progress" style="font-size: 60%; position:relative; border: 1px solid #c8ccd1; background-color: #f0f0f0; width: 100px; max-width: 100%; height: 10px;"><div style="position: relative; background-color:#00af32; width: ' + per + '%; height: 10px;"></div><div style="position: absolute; font-weight: high; width:100%; height: 10px; text-align: center; color: #000;"></div></div>');
}
});
}
}());