Fandom

Wikia Developers Wiki

SpoilerAlert

888pages on
this wiki
Add New Page
Talk133 Share

SpoilerAlert hides the content area of a page and displays a dialog that asks the visitor if he wants to risk seeing spoilers or not. If not the page will remain hidden. If the visitor accepts, the foreground fades away and reveals the page. Additionally a cookie is set, to ensure the visitor won't see this dialog for this particular page again.

You can see a simple demo here.

The dialog itself looks like this:

This page contains spoilers. Are you sure you want to read it?
No, not yet
Yes, please

(The buttons are not styled in any way; they will look like all other buttons on your wiki. The color of the background depends on your wiki as well. It will be the same as the background color of your content area.)

SpoilerAlert 2

SpoilerAlert 2 is a modified version of SpoilerAlert.

Installation

Add this JavaScript on your wiki:

Site-wide
MediaWiki:Common.js
SpoilerAlert = {
  pages: ["Spoiler"],
}
importArticles({
    type: "script",
    articles: [
        "w:c:dev:SpoilerAlert/code.2.js"
    ]
});
importArticles — Best Practices for installing JavaScript on Fandom
The importArticles statement is designed to combine multiple HTTP requests into a single data transfer, allowing multiple scripts to load and execute faster. If you've been installing several different scripts, your JavaScript file has probably accumulated unnecessary import statements. Click "Expand" to learn how to efficiently batch import scripts to speed up performance and make your code look cleaner. One other approach is by using the MediaWiki:ImportJS.
If your JavaScript file has several lines of code that say importScript, importScriptPage, or importArticles, you may be able to combine them! By batch importing a collection of scripts with a single import, your JavaScript code will load faster and look cleaner. Consider the example below. On the left is an example of what your JavaScript file might currently look like. On the right is how you could improve that code.
Multiple imports — messy and slow One import — clean and efficient
importScriptPage('AjaxRC/code.js','dev');
 
importScript('MediaWiki:localScript.js');
 
importArticle({
  type: 'script',
  article: 'u:dev:FloatingToc/code.js'
});
 
importScriptPage('page1.js', 'wikiname');
 
importScriptPage('page2.js', 'wikiname');
importArticles({
    type: 'script',
    articles: [
        'u:dev:AjaxRC/code.js',
        'MediaWiki:localScript.js',
        'u:dev:FloatingToc/code.js',
        'u:wikiname:page1.js',
        'u:wikiname:page2.js'
    ]
});
Note: In this example, pay close attention to the placement of commas and other punctuation. For people who aren't familiar with programming (and even those who are!), a common mistake when writing code is to accidentally delete, forget, or misplace critical symbols like commas or quotation marks. This can cause a syntax error that breaks the code. Carefully follow the convention shown here when using importArticles.
But there's much more to importArticles than just this! For more examples and advanced usage, see the help page at Help:Including additional JavaScript and CSS.


Configuration

Spoiler alert on specific pages

To add the spoiler alert on specific pages, just add this before the import statements:

SpoilerAlert = {
  pages: ["Page 1", "Page 2", "Page 3"],
}

And replace Page 1, Page 2 etc... by the pages you want.

Spoiler alert on pages of a specific category

SpoilerAlert = {
  categories: "Spoiler",
}

And replace Spoiler etc... by the category you want.

Spoiler alert on pages that contains an object with a specific class

SpoilerAlert = {
  'class': "Spoiler",
}

And replace Spoiler etc... by the class you want. For example, with Spoiler, if you have in your page :

<span class="Spoiler"></span>

Then, the spoiler alert will be shown.

Improvements

You have the opportunity to clear storage reminding for the current spoiler page by clicking the button next to the "Discussion" button at the top of the page.

Installation

Add this line to your JavaScript page of choice:

importArticles({
    type: "script",
    articles: [
        "w:c:dev:SpoilerAlert/code.js"
    ]
});
importArticles — Best Practices for installing JavaScript on Fandom
The importArticles statement is designed to combine multiple HTTP requests into a single data transfer, allowing multiple scripts to load and execute faster. If you've been installing several different scripts, your JavaScript file has probably accumulated unnecessary import statements. Click "Expand" to learn how to efficiently batch import scripts to speed up performance and make your code look cleaner. One other approach is by using the MediaWiki:ImportJS.
If your JavaScript file has several lines of code that say importScript, importScriptPage, or importArticles, you may be able to combine them! By batch importing a collection of scripts with a single import, your JavaScript code will load faster and look cleaner. Consider the example below. On the left is an example of what your JavaScript file might currently look like. On the right is how you could improve that code.
Multiple imports — messy and slow One import — clean and efficient
importScriptPage('AjaxRC/code.js','dev');
 
importScript('MediaWiki:localScript.js');
 
importArticle({
  type: 'script',
  article: 'u:dev:FloatingToc/code.js'
});
 
importScriptPage('page1.js', 'wikiname');
 
importScriptPage('page2.js', 'wikiname');
importArticles({
    type: 'script',
    articles: [
        'u:dev:AjaxRC/code.js',
        'MediaWiki:localScript.js',
        'u:dev:FloatingToc/code.js',
        'u:wikiname:page1.js',
        'u:wikiname:page2.js'
    ]
});
Note: In this example, pay close attention to the placement of commas and other punctuation. For people who aren't familiar with programming (and even those who are!), a common mistake when writing code is to accidentally delete, forget, or misplace critical symbols like commas or quotation marks. This can cause a syntax error that breaks the code. Carefully follow the convention shown here when using importArticles.
But there's much more to importArticles than just this! For more examples and advanced usage, see the help page at Help:Including additional JavaScript and CSS.

Triggers

By default SpoilerAlert triggers when the word "spoiler" is part of the page name. That may be useful for some of you, but others may have different ideas. And therefore you can supply your own function for the trigger. Let's look at a few examples that should cover most scenarios:

This function triggers the SpoilerAlert if there's some element of CSS-class "spoiler" on the page:

SpoilerAlert = {
    isSpoiler: function () {
        return Boolean($('.spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

The function's name must be "isSpoiler" and it should return true if the page is a spoiler page and false otherwise.

This function checks whether the page is in the category "Spoiler"

SpoilerAlert = {
    isSpoiler: function () {
        return -1 !== wgCategories.indexOf('Spoiler');
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

And lastly here's a function that will check all H2 elements for the text 'Spoiler':

SpoilerAlert = {
    isSpoiler: function () {
        var h2s = document.getElementsByTagName('h2');
        for (var i = 0, c = h2s.length; i < c; i++) {
            if (/spoiler/i.test($(h2s[i]).text())) return true;
        }
        return false;
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

Language Options

You can override the text in the dialog by setting the "question", "yes" and "no" properties.

This example translates the dialog into German and displays it, if there's an element with the ID "spoiler":

SpoilerAlert = {
    question: 'Diese Seite enthält Spoiler. Möchten Sie sie trotzdem ansehen?',
    yes: 'Aber ja doch',
    no: 'Nein, noch nicht',
    isSpoiler: function () {
        return Boolean($('#spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

This one, into French.

SpoilerAlert = {
    question: 'Cette page contient pas mal de spoilers. Voulez-vous toutefois continuer ?',
    yes: 'Oui s\'il vous plaît',
    no: 'Non, pas question',
    isSpoiler: function () {
        return Boolean($('#spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

This one, into Russian.

SpoilerAlert = {
    question: 'Эта страница содержит спойлеры. Хотите ли вы продолжить чтение?',
    yes: 'Да',
    no: 'Нет, не сейчас',
    isSpoiler: function () {
        return Boolean($('#spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

This one, into Ukrainian.

SpoilerAlert = {
    question: 'Ця сторінка містить спойлери. Чи хочете ви продовжити читання?',
    yes: 'Так',
    no: 'Ні, не зараз',
    isSpoiler: function () {
        return Boolean($('#spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

This one, into Belarusian.

SpoilerAlert = {
    question: 'Гэтая старонка змяшчае спойлеры. Ці вы хочаце працягнуць чытанне?',
    yes: 'Так',
    no: 'Не, не цяпер',
    isSpoiler: function () {
        return Boolean($('#spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

This one, into Brazilian Portuguese.

SpoilerAlert = {
    question: 'Esta página contém spoilers. Você tem certeza de que você quer ler isto?',
    yes: 'Sim, por favor',
    no: 'Não, ainda não',
    isSpoiler: function () {
        return Boolean($('#spoiler').length);
    }
};
importScriptPage('SpoilerAlert/code.js', 'dev');

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.