Wikipedia:User scripts/Guide: Difference between revisions

Content deleted Content added
Dantman (talk | contribs)
portlets: Updating portlet code
Line 266:
</div>
 
There is a special function in [{{SERVER}}[:mw:ResourceLoader/skins-1Default_modules#addPortletLink|mediawiki.5/common/wikibits.js wikibitsutil.js]] that simplifies the process of adding your own links into portlets:<br/>
<tt>mw.util.'''addPortletLink''' (portlet, href, text, id, tooltip, accesskey, nextnode)</tt>
 
<source lang="javascript">
// Several examples of portlet links
addPortletLink ('p-tb', wgArticlePath.replace('$1', 'Special:MyPage/vector.js'), 'My vector.js', 'pt-myvector', 'Visit your vector.js file');
addPortletLink ('p-personal', wgScript + '?title=User:' + encodeURIComponent(wgUserName) + '/Notes&action=edit', 'My notes', 'pt-mynotes', 'Edit your personal notes');
addPortletLink ('p-tb', wgArticlePath.replace('$1', 'Special:Prefixindex/'+wgPageName), 'Prefixindex', 'tb-prefixindex');
addPortletLink ('p-personal', '/wiki/Special:Log/' + encodeURIComponent(wgUserName), 'My logs', 'pt-mylogs');
</source>
 
// Adds a link to your Vector.js file to the toolbox
Last 4 arguments are optional; all arguments are explained in the code.
mw.util.addPortletLink ( 'p-tb', mw.util.wikiGetlink( 'Special:MyPage/vector.js' ),
* <tt>id</tt> of your new link (in case you need to access it later with <tt>getElementById</tt>)
addPortletLink ('p-tb', wgArticlePath.replace('$1', 'Special:MyPage/vector.js'), 'My vector.js', 'pt-myvector', 'Visit your vector.js file');
* <tt>tooltip</tt>
 
* <tt>accesskey</tt>
// Add a link to the edit page for your Notes in your personal links
* <tt>nextnode</tt> — if you want to insert new link before another element, in our example this could be <code>document.getElementById('t-specialpages')</code>, i.e. «Special pages» link
// Note: We assume that short/pretty URLs are in use with ?action, ideally you
// would check for that.
mw.util.addPortletLink ( 'p-personal', mw.util.wikiGetlink( 'Special:MyPage/Notes' ) + '?action=edit',
'My notes', 'pt-mynotes', 'Edit your personal notes' );
 
// Adds a link to prefix index for the current page to the toolbox
mw.util.addPortletLink ( 'p-tb',
mw.util.wikiGetlink( 'Special:Prefixindex/' + mw.config.get( 'wgPageName' ) ),
'Prefixindex', 'tb-prefixindex');
// Adds a link to
mw.util.addPortletLink ( 'p-personal',
mw.util.wikiGetlink( 'Special:Log/' + mw.config.get( 'wgUserName' ) ),
'My logs', 'pt-mylogs');
</source>
 
=== Adding elements ===