Fandom

Community Central

Admin Forum:JS code to lock old blogs

19,026pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

This Forum has been archived

Forums: Admin Central Index Technical Help JS code to lock old blogs
Central's forums are a place for the community to help other members.
To contact staff directly or to report bugs, please use Special:Contact.
Note: This topic has been unedited for 1365 days. It is considered archived - the discussion is over. Do not add to unless it really needs a response.


I'd like to know if there's a way I can tweak this code to lock only blogs sorted in a given category:

* by: [[User:Joeyaa|Joey Ahmadi]]
*/

$(function() {
if (wgNamespaceNumber == 500 && $('#article-comments-ul li').size() > 1) {
var then = $('#article-comments-ul > .SpeechBubble:first .permalink').attr('href');
then = new String(then.match(/\d{8}/));
var monthnames = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
var year = then.match(/^\d{4}/);
var month = then.substring(4,6); 
month--;
month= monthnames[month];
var day = then.match(/\d{2}$/);
then = new Date(month+''+day+', '+year); 
var old = parseInt(now - then);
old = Math.floor(old/(1000*60*60*24));
if (old > 30) {
$('#article-comm').attr('disabled','disabled').text('This blog post hasn\'t been commented on for over 30 days. There is no need to comment.');
$('#article-comm-submit').attr('disabled','disabled');
$('.article-comm-reply').remove();
}
}
});

For example, only blogs categorized in "Blog posts". ― Thailog 14:37, March 2, 2012 (UTC)

Looks like you would only have to modify the if-clause that contains everything else:
if (-1 < $.inArray("Blog posts", wgCategories) && wgNamespaceNumber == 500 && $('#article-comments-ul li').size() > 1) { /* ... */ }


Pecoes 15:54, March 2, 2012 (UTC)

Hmm, it still locks blogs not categorized there... Am I doing something wrong? ― Thailog 17:56, March 2, 2012 (UTC)

Maybe. Are you sure you've added the new bit in the right place? -- Pecoes 18:31, March 2, 2012 (UTC)
I thought the { /* ... */ } bit was supposed to stay. Fail. It works now, thanks! What if I want to add more categories? Will "Blog posts", "Other Category", wgCategorie work? ― Thailog 20:13, March 2, 2012 (UTC)
I was afraid you'd ask me that. That's slightly more complicated.
Try this:
function  isCategoryBlacklisted (blacklist) {
    for (var i = 0; i < wgCategories.length; i++)
        if (-1 < $.inArray(wgCategories[i], blacklist ))
            return true;
    return false;
}
 
if (isCategoryBlacklisted(["Blog posts", "Other Category"]) && wgNamespaceNumber == 500 && $('#article-comments-ul li').size() > 1) { /* ... */ }


Nope. :( ― Thailog 21:12, March 2, 2012 (UTC)
Hm. Are you sure? Here's my test page:
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
 
wgCategories = [5,4];
 
function isCategoryBlacklisted (blacklist) {
    for (var i = 0; i < wgCategories.length; i++)
        if (-1 < $.inArray(wgCategories[i], blacklist ))
            return true;
    return false;
}
 
blacklist = [1,2,3,4];
 
console.log(isCategoryBlacklisted(blacklist));
 
blacklist = [6,1,2];
 
console.log(isCategoryBlacklisted(blacklist));
 
</script>
</head>
<body></body>
</html>


The first console.log() outputs "true" and the second "false" -- Pecoes 21:28, March 2, 2012 (UTC)
Unless I messed it up. Do you mind taking a peak? ― Thailog 21:38, March 2, 2012 (UTC)

You accidentally removed an important line from your script:

function  isCategoryBlacklisted (blacklist) {
    for (var i = 0; i < wgCategories.length; i++)
        if (-1 < $.inArray(wgCategories[i], blacklist ))
            return true;
    return false;
}
 
$(function() { 
    if (isCategoryBlacklisted(["Blog posts", "Site news"]) && wgNamespaceNumber == 500 && $('#article-comments-ul li').size() > 1) {
        var then = $('#article-comments-ul > .SpeechBubble:first .permalink').attr('href');
        then = new String(then.match(/\d{8}/));
        var monthnames = ['January','February','March','April','May','June','July',
        'August','September','October','November','December'];
        var year = then.match(/^\d{4}/);
        var month = then.substring(4,6); 
        month--;
        month= monthnames[month];
        var day = then.match(/\d{2}$/);
        then = new Date(month+''+day+', '+year); 
        var old = parseInt(now - then);
        old = Math.floor(old/(1000*60*60*24));
        if (old > 30) {
            $('#article-comm').attr('disabled','disabled').text('This blog post hasn\'t been commented on for over 30 days. There is no need to comment.');
            $('#article-comm-submit').attr('disabled','disabled');
            $('.article-comm-reply').remove();
        }
    }
});

It's the line that says: $(function() {. Without that line the script is broken.

I gotta ask: You don't know much about JavaScript, do you? -- Pecoes 22:32, March 2, 2012 (UTC)

Do you even have to ask? Not. A. Thing. (Unless you count making very small adjustments to already existing codes). Is there any beginners tutorial with idiotproof instructions I could benefit from? :) Btw, now it works, so thanks a bunch! ― Thailog 22:46, March 2, 2012 (UTC)

No, sorry! I'm sure there's lots of good beginner's tutorials out there, but I'm no longer their target group. I suppose the w3schools tutorial doesn't suck, but there might be much better ones... -- Pecoes 23:09, March 2, 2012 (UTC)

Also on Fandom

Random Wiki