-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript.js
More file actions
159 lines (129 loc) · 4.69 KB
/
Copy pathuserscript.js
File metadata and controls
159 lines (129 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// ==UserScript==
// @name Tweakers.net Markeren als gelezen
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Verberg al het nieuws op de homepage wat je al gelezen hebt of niet wilt lezen
// @author Tomas van Rijsse
// @match tweakers.net/*
// @grant none
// @require https://code.jquery.com/jquery-3.4.1.slim.min.js
// ==/UserScript==
/* jshint -W097 */
/* global jQuery */
var json = localStorage.getItem('articles') || "{}",
articles = JSON.parse(json);
jQuery(function($){
$('body').append('<style>'+
'.headlineItem.selected { background-color: rgba(0,0,0,0.2); }'+
'.headlineItem.selected article.headline a {opacity:0.5}'+
'.headlineItem.hiddenForMe article.headline a {opacity:0.3}'+
'</style>');
markAsRead();
if(document.location.pathname == '/'){
var home = new Home($);
var target = document.getElementsByClassName('headlines')[0];
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
home.hideArticles();
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
}
});
function markAsRead(){
var id = getLinkId(document.location.href);
if( getLinkId(document.location.href) !== null ){
articles[id]= true;
localStorage.setItem('articles',JSON.stringify(articles));
}
}
var Home = function($){
var self = this;
self.isMouseDown = false;
self.hideArticles();
$(".headlineItem").mousedown(function () {
self.isMouseDown = true;
$(".headlineItem").removeClass("selectionStart selected selectionEnd");
$(this).addClass("selectionStart selected");
return false; // prevent text selection
})
.mouseenter(function () {
if (self.isMouseDown) {
$(".headlineItem").removeClass('selectionEnd');
$(this).addClass("selectionEnd");
self.selectInBetween($);
}
})
.bind("selectstart", function () {
return false; // prevent text selection in IE
});
$(document).mouseup(function () {
self.isMouseDown = false;
$(".headlineItem").removeClass('selectionStart selectionEnd');
})
.keyup(function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code==13 && $(".headlineItem.selected").length) {
e.preventDefault();
$(".headlineItem.selected").each(function(){
var id = getLinkId($(this).find('article.headline a').prop('href'));
if(id){
articles[id] = true;
}
}).removeClass('selected').addClass('hiddenForMe');
localStorage.setItem('articles',JSON.stringify(articles));
}
if (code==27 && $(".headlineItem.selected").length) {
e.preventDefault();
$(".headlineItem.selected").removeClass('selected');
}
});
};
Home.prototype = {
hideArticles:function(){
/* hide articles on load */
$('.headlineItem').each(function(){
var $tr = $(this),
id = getLinkId($tr.find('a').first().prop('href'));
if(id === null) return true;
if(articles.hasOwnProperty(id)){
$tr.addClass('hiddenForMe');
}
});
},
selectInBetween: function($){
var $headlines = $('.headlineItem'),
inBetweeners = [],
startFound = false,
revertedSelection = false;
$headlines.each(function() {
if( $(this).hasClass('selectionStart')) { startFound = true }
if( $(this).hasClass('selectionEnd') && !startFound ) { startFound = true; revertedSelection = true }
if(startFound) {
inBetweeners.push(this);
}
if(($(this).hasClass('selectionEnd') && !revertedSelection) ||
($(this).hasClass('selectionStart') && revertedSelection))
{
return false
}
});
$(inBetweeners).addClass('selected');
$('.headlineItem').not($(inBetweeners)).removeClass('selected');
}
};
function getLinkId(link){
if(!link){
return null
}
var newsRegex = RegExp('\.net\/([a-z]{4,20})\/([0-9]{4,6})','');
var ids = link.match(newsRegex);
if(ids === null){
return null;
}
return ids[1]+ids[2];
}