Usuário:Brandizzi/aeditar.js

Origem: Wikipédia, a enciclopédia livre.

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
// Æditar: a script to register pages you want to edit.
// Author: [[Usuário:Brandizzi]]
 
// if user has not set a custom user page, use default.
if (typeof aeditarListPage == 'undefined')
    aeditarListPage = 'A_Editar';
var aeditarUserNamespace = 'Usuário'
 
/* Add a link to the current article at the to edit page */
function aeditarAddLink() {
    if (aeditarIsEditable()) {
        var element = document.getElementById('contentSub')
        element.appendChild(aeditarCreateSimpleLink());
        element.appendChild(aeditarCreateLinkWithNote());
    }
}
 
/* Put the link on the edit textarea and a command on the summary */
function aeditarAddContent(title, note) {
    var decodedTitle = decodeURI(title);
    var articleLink = '[[' + decodedTitle + ']]';
 
    var current = document.getElementById('wpTextbox1').value;
    var updated = current + '* ' + articleLink;
 
    if (note != null)
        updated = updated + " (" + decodeURI(note) + ")";
    document.getElementById('wpTextbox1').value = updated;
 
    document.getElementById('wpSummary').value =
        'Adding ' + articleLink + ' to pages to edit with [[Usuário:Brandizzi/Æditar|Æditar]]';
}
 
/* Submit the edit form */
function aeditarClickSubmit() {
    document.getElementById('wpSave').click()
}
 
/* Gets the value of the given query variable */
function aeditarGetQueryVariableValue(uri, variable) {
    var paramsArray = uri.split('&');
    for (var i = 0; i < paramsArray.length; i++) {
        var key = paramsArray[i].split('=')[0];
        var value = paramsArray[i].split('=')[1];
        if (key == variable)
            return value;
    }
    return null;
}
 
/* Return the the title of the article to be edited. This value
   is only present when the link of Æditar is clicked. */
function aeditarGetSentTitle(uri) {
    return aeditarGetQueryVariableValue(uri, 'aeditartitle');
}
 
/* Return a note about the article to be edited, if the user sent it. */
function aeditarGetSentNote(uri) {
    return aeditarGetQueryVariableValue(uri, 'aeditarnote');
}
 
/* Verifies if the page is eligible for edition */
function aeditarIsEditable(uri) {
    return wgCanonicalNamespace != 'Special';
}
 
/* Create a link within a small tag */
function aeditarCreateSimpleLink() {
    // Create a link
    var link = document.createElement('a');
    link.setAttribute('href', aeditarGetURLForAppending());
    link.appendChild(document.createTextNode('a editar'));
 
    // put on a small element
    var small = document.createElement('small');
    small.appendChild(document.createTextNode(' ['));
    small.appendChild(link)
    small.appendChild(document.createTextNode(']'));
 
    return small;
}
 
/* Create a link to the list of to-edit pages */
function aeditarCreateLinkToList() {
    var pageName = aeditarUserNamespace + ':'  + wgUserName + '/' + aeditarListPage;
    var url = '/wiki/' + pageName;
    // Create a link
    var link = document.createElement('a');
    link.setAttribute('href', url);
    link.appendChild(document.createTextNode('páginas a editar'));
 
    // put on a small element
    var li = document.createElement('li');
    li.setAttribute('id', 'pt-toeditlist');
    if (wgPageName == pageName) {
        li.setAttribute('class', 'active');
    }
    li.appendChild(link)
 
    return li;
}
 
/* Add the link to the pages of articles to edit to the top of the page */
function aeditarAddLinkToList() {
    var link = aeditarCreateLinkToList();
    var logoutLink = document.getElementById('pt-logout');
    logoutLink.parentNode.insertBefore(link, logoutLink);
}
 
function aeditarGetURLForAppending() {
    // Retrieve the title
    var title = encodeURI(wgTitle).replace(/ /g, '%20');
    return 'http://pt.wikipedia.org/w/index.php?title=' +
        aeditarUserNamespace +':' + wgUserName + '/' + aeditarListPage +
        '&action=edit&aeditartitle='+ title
}
 
/* Create a link within a small tag */
function aeditarCreateLinkWithNote() {
    // Retrieve the title
    var title = encodeURI(wgTitle).replace(/ /g, '%20');
 
    // Create a link
    var link = document.createElement('a');
    link.setAttribute('href', '#');
    link.setAttribute('onclick', 'javascript:aeditarSubmitContentWithNote()');
    link.appendChild(document.createTextNode('com nota'));
 
    // put on a small element
    var small = document.createElement('small');
    small.appendChild(document.createTextNode(' ['));
    small.appendChild(link)
    small.appendChild(document.createTextNode(']'));
 
    return small;
}
 
function aeditarSubmitContentWithNote() {
    var url = aeditarGetURLForAppending();
    var note = prompt("Nota:", "");
    if (note != null && note != '') {
        var encodedNote = encodeURI(note).replace(/ /g, '%20');
        url = url + '&aeditarnote=' + encodedNote;
        window.location = url;
    }
}

function addOnloadHookIfAvailable(f) {
    if (typeof addOnloadHook == "undefined") { 
        console.info("addOnloadHook not available");
        return;
    }
    addOnloadHook(f);
}
addOnloadHookIfAvailable(function () {
    // Add a link to the list before the logout link
    aeditarAddLinkToList();
    // Tries to get the value of 'aeditarcontent' from the URL.
    var aeditarPageTitle = aeditarGetSentTitle(window.location.search);
 
    if (aeditarPageTitle == null) {
        // If such parameter is not found on the URL, then we are not submitting
        // a page to be saved on the list; add the link to submission.
        aeditarAddLink();
    } else {
        // If the value is set, so we are saving a new page on the list; add the
        // content and submit it.
        var note = aeditarGetSentNote(window.location.search);
        aeditarAddContent(aeditarPageTitle, note);
        aeditarClickSubmit();
    }
});