1 | /** |
||
2 | * MW Api handler to create article content from data-content-selector |
||
3 | */ |
||
4 | |||
5 | /*global jQuery, mediaWiki */ |
||
6 | /*global confirm */ |
||
7 | |||
8 | ( function ( $, mw ) { |
||
9 | |||
10 | 'use strict'; |
||
11 | |||
12 | $( function ( $ ) { |
||
13 | |||
14 | var page = { |
||
15 | |||
16 | /** |
||
17 | * @since 1.0 |
||
18 | * |
||
19 | * MW Api handler |
||
20 | */ |
||
21 | api: new mw.Api(), |
||
22 | |||
23 | /** |
||
24 | * @since 1.0 |
||
25 | * |
||
26 | * Create article |
||
27 | */ |
||
28 | create: function ( title, content ) { |
||
29 | |||
30 | this.api.postWithToken( "csrf", { |
||
31 | action: "edit", |
||
32 | title: title, |
||
33 | section: 0, // Existing content will be replaced |
||
34 | // summary: section, // no need for a section heading |
||
35 | text: content |
||
36 | } ).done( function( result, jqXHR ) { |
||
0 ignored issues
–
show
|
|||
37 | location.reload(); |
||
38 | // $( '#scite-status' ).append( 'Added: ' + title ); // not sure we need an update not |
||
39 | } ).fail( function( xhr, status, error ) { |
||
0 ignored issues
–
show
|
|||
40 | |||
41 | var apiErrorText = ''; |
||
42 | |||
43 | if ( xhr === "http" ) { |
||
44 | apiErrorText = "HTTP error: " + status.textStatus; // status.xhr contains the jqXHR object |
||
45 | } else if ( xhr === "ok-but-empty" ) { |
||
46 | apiErrorText = "Got an empty response from the server"; |
||
47 | } else { |
||
48 | apiErrorText = "API error: " + xhr; |
||
49 | } |
||
50 | |||
51 | if ( status.hasOwnProperty( 'xhr' ) ) { |
||
52 | apiErrorText = status.xhr.responseText.replace(/\<br \/\>/g," "); |
||
53 | var xhr = status.xhr; |
||
54 | |||
55 | if ( xhr.hasOwnProperty( 'responseText' ) ) { |
||
56 | apiErrorText = xhr.responseText.replace(/\<br \/\>/g," " ); |
||
57 | }; |
||
58 | |||
59 | if ( xhr.hasOwnProperty( 'statusText' ) ) { |
||
60 | apiErrorText = 'The API returned with: ' + xhr.statusText.replace(/\<br \/\>/g," " ); |
||
61 | }; |
||
62 | } |
||
63 | |||
64 | $( '#scite-status' ).append( apiErrorText ); |
||
65 | } ); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | $( '.scite-create' ).on( 'click', function( event ) { |
||
70 | |||
71 | // Dynamically select element that contains the content |
||
72 | // to be copied |
||
73 | page.create( |
||
74 | $( this ).data( 'title' ), |
||
75 | $( $( this ).data( 'content-selector' ) ).text() |
||
76 | ); |
||
77 | |||
78 | event.preventDefault(); |
||
79 | } ); |
||
80 | |||
81 | } ); |
||
82 | }( jQuery, mediaWiki ) ); |
||
83 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.