Last active
December 29, 2015 15:59
-
-
Save megatolya/05307c3595e6c82e60fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name metrika logs helper | |
// @namespace https://metrika.yandex.ru/ | |
// @description Позволяет скрывать ошибки на странице параметры визитов | |
// @include https://metrika.yandex.ru/stat/user_vars/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
var $ = document.querySelector.bind(document); | |
var cache = function(key, val) { | |
if (val === undefined) | |
return localStorage.getItem(key); | |
localStorage.setItem(key, val); | |
}; | |
var REMOVE_ROW = 'убрать'; | |
var RECOVER_ROW = 'вернуть'; | |
var ignoreWords = ['Ещё']; | |
if (document.body) { | |
init(); | |
} else { | |
window.onload = init; | |
} | |
function init() { | |
var data; | |
try { | |
data = JSON.parse(cache('metrika')) ; | |
} catch (err) { | |
data = {}; | |
} | |
data = data || {}; | |
/* | |
data obj example: | |
{ | |
'2.8.0': { | |
{ | |
'Notify listener error: TypeError: aWindow is undefined': { | |
visible: true | |
} | |
} | |
} | |
*/ | |
var table = $('.b-data-table_type_dynamic'); | |
[ | |
$('.b-placeholder'), | |
$('.b-chart-report-user-vars') | |
].forEach(function(node) { | |
node && node.remove(); | |
}); | |
var style = document.createElement('style') ; | |
style.innerHTML = [ | |
'.level1 .b-pseudo-link{display:inline-block;max-width: 550px;overflow:hidden;text-overflow:clip;}', | |
'.wrap{width:570px;}', | |
'.wrap .b-data-table__checkbox{top:-3px;}' | |
].join(''); | |
document.head.appendChild(style); | |
function getData(row) { | |
var name = row.querySelector('.b-pseudo-link').innerHTML; | |
var rowData = data[row.dataset.version][name]; | |
if (!rowData) { | |
rowData = { | |
visible: true | |
}; | |
data[row.dataset.version][name] = rowData; | |
} | |
cache('metrika', JSON.stringify(data)); | |
return { | |
name: name, | |
visible: rowData.visible | |
}; | |
} | |
// позиционирование строчки | |
function processRow(row, ignoreVisible) { | |
var rowData = getData(row); | |
if (rowData.visible) { | |
if (!ignoreVisible) { | |
var nearest = findNearestRow(row); | |
nearest.parentNode.insertBefore(row, nearest); | |
} | |
} else { | |
table.querySelector('table').appendChild(row); | |
} | |
row.style.opacity = rowData.visible ? '1' : '0.3'; | |
} | |
function findNearestRow(row) { | |
var rows = table.querySelectorAll('tbody'); | |
var currentRow = rows[0]; | |
for (var i = 0; i < rows.length; i++) { | |
if (!rows[i].dataset.index) | |
continue; | |
if (parseInt(rows[i].dataset.index, 10) < parseInt(row.dataset.index, 10)) { | |
// учитываем только показанные | |
if (getData(rows[i]).visible) | |
currentRow = rows[i]; | |
} | |
} | |
return currentRow.nextSibling; | |
} | |
function toggleVisibility(row) { | |
var rowData = getData(row); | |
var name = row.querySelector('.b-pseudo-link').innerHTML; | |
rowData.visible = !rowData.visible; | |
data[row.dataset.version][name] = rowData; | |
cache('metrika', JSON.stringify(data)); | |
var link = row.querySelector('.toggle'); | |
link.innerHTML = rowData.visible ? REMOVE_ROW : RECOVER_ROW; | |
processRow(row); | |
} | |
var observer = new MutationObserver(function(mutations) { | |
var rows = table.querySelectorAll('tbody'), | |
currentLevel = ''; | |
for (var i = 0; i < rows.length; i++) { | |
var row = rows[i]; | |
if (row.dataset.inited) | |
continue; | |
if (row.classList.contains('level2')) { | |
continue; | |
} | |
if (row.classList.contains('level0')) { | |
currentLevel = row.querySelector('.b-pseudo-link').innerHTML; | |
continue; | |
} | |
data[currentLevel] = data[currentLevel] || {}; | |
row.dataset.inited = 'yes'; | |
row.dataset.version = currentLevel; | |
row.dataset.index = i; | |
if (row.classList.contains('level1')) { | |
var desc = row.querySelector('td.align-left'); | |
var pseudoLink = row.querySelector('.b-pseudo-link'); | |
// оборачиваем td, чтобы обрезать содержимое | |
desc.innerHTML = '<div class="wrap">' + desc.innerHTML + '</div>'; | |
// выставляем title, чтобы можно было видеть исходное значение | |
pseudoLink.setAttribute('title', pseudoLink.innerHTML); | |
} | |
var td = row.querySelector('td'); | |
var link = document.createElement('a'); | |
link.classList.add('toggle'); | |
link.style.textDecoration='none'; | |
link.style.color='black'; | |
link.style.borderBottom='1px dotted black'; | |
link.onmouseover = function() { | |
this.style.borderBottom='1px dotted red'; | |
}; | |
link.onmouseout = function() { | |
this.style.borderBottom='1px dotted black'; | |
}; | |
link.onclick = function() { | |
this.style.borderBottom='1px dotted black'; | |
}; | |
link.href='#'; | |
var name = row.querySelector('.b-pseudo-link').innerHTML; | |
if (ignoreWords.indexOf(name) !== -1) | |
continue; | |
(function(row) { | |
link.addEventListener('click', function(e) { | |
e.preventDefault(); | |
toggleVisibility(row); | |
}); | |
})(row); | |
td.appendChild(link); | |
link.innerHTML = getData(row).visible ? REMOVE_ROW : RECOVER_ROW; | |
processRow(row, true); | |
} | |
}); | |
var config = { | |
childList: true, | |
subtree: true | |
}; | |
observer.observe(table, config); | |
cache('metrika', JSON.stringify(data)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment