Jump to content
Wikipedia The Free Encyclopedia

User:Novem Linguae/Scripts/CentralNoticeHelper.js

From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Novem Linguae/Scripts/CentralNoticeHelper.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 // <nowiki>
 // Requested by Pharos for metawiki
 // Generates some Special:CentralNoticeBanner geolocation code that otherwise has to be generated manually and adjusted

 classCentralNoticeCodeHelper{
 constructor(mw,$){
 this.mw=mw;
 // eslint-disable-next-line no-jquery/variable-pattern
 this.$=$;
 }

 asyncexecute(){
 this.mw.util.addPortletLink('p-cactions','#','CentralNotice Helper','central-notice-code-helper');

 this.$('#central-notice-code-helper').on('click',()=>{
 this.loadForm();
 });
 }

 loadForm(){
 conststates=['AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'];
 constprovinces=['AB','BC','MB','NB','NL','NT','NS','NU','ON','PE','QC','SK','YT'];
 conststatesAndProvinces=states.concat(provinces).sort();
 conststatesHtml=statesAndProvinces.map((state)=>`<option value="${state}">${state}</option>`).join('');

 this.$('#firstHeading').html('CentralNotice Helper');
 this.$('#bodyContent').html(`

 			State:
 			<select>
 ${statesHtml}
 			</select>
 			<br />

 			<select name="where">
 				<option value="north-of">north-of</option>
 				<option value="south-of">south-of</option>
 				<option value="east-of">east-of</option>
 				<option value="west-of">west-of</option>
 			</select>
 			<input type="text" name="latitude-or-longitude" placeholder="36.5" /><br />

 			<input type="button" value="Generate code" id="generate-code-button">

 			<div id="central-notice-helper-result"></div>

 		`);

 this.$('#generate-code-button').on('click',()=>{
 if(this.$('[name="latitude-or-longitude"]').val()===''){
 this.$('#central-notice-helper-result').html('Please enter a latitude or longitude.');
 return;
 }else{
 this.loadResult();
 }
 });
 }

 loadResult(){
 conststate=this.$('#bodyContent select').val();
 consttype=['north-of','south-of'].includes(this.$('[name="where"]').val())?'lat':'lon';
 constdecimal=this.$('[name="latitude-or-longitude"]').val();

 consthtml=`<div class="cnotice georegion georegion-${state}-subregion" id="${state}-subregion" dir="ltr">`;
 constjavaScript=
 `<script type="text/javascript">
 (function(){
  var region = mw.centralNotice.data.region;
  if( region == '${state}' && Geo.${type} <= ${decimal} ) {
  region = '${state}-subregion';
  }
  var div = $( 'div.georegion.georegion-' + region )
  if( div.length ) {
  div.addClass('georegionselected');
  $( 'div#${state}-subregion' ).show();
  } else {
  mw.centralNotice.bannerData.hideResult = true;
  mw.centralNotice.bannerData.hideReason = 'outofscope';
  }
 })()
 </script>`;

 this.$('#central-notice-helper-result').html(`

 			<br /><br />

 			HTML:<br />
 			<textarea>${html}</textarea>

 			JavaScript:<br />
 			<textarea style="height:20em;">${javaScript}</textarea>

 		`);
 }
 }

 $(async()=>{
 awaitmw.loader.using(['mediawiki.util'],async()=>{
 await(newCentralNoticeCodeHelper(mw,$)).execute();
 });
 });

 // </nowiki>

AltStyle によって変換されたページ (->オリジナル) /