Jump to content
Wikipedia The Free Encyclopedia

User:Opencooper/lastEdit.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.
This user script seems to have a documentation page at User:Opencooper/lastEdit.
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.
 // This script reimplements the last edit banner used on mobile

 // License: CC0

 functionrequestRevision(){
 // If we're not reading an article, do nothing
 if(!(mw.config.get("wgAction")==="view"
 &&mw.config.get("wgIsArticle")
 &&!mw.config.get("wgIsMainPage"))){
 return;
 }

 // API docs: https://www.mediawiki.org/wiki/API:Revisions
 varapiUrl=location.origin+"/w/api.php";
 $.ajax({
 url:apiUrl,
 data:{
 action:"query",
 prop:"revisions",
 format:"json",
 titles:mw.config.get("wgPageName"),
 rvprop:"timestamp|user|comment|tags|ids"
 },
 success:parseEdit
 });
 }

 functionparseEdit(response){
 varpageId=mw.config.get("wgArticleId");
 try{
 varpageInfo=response.query.pages[pageId].revisions[0];
 }catch(e){
 return;
 }
 varrelativeTimestamp=calculateRelativeTimestamp(pageInfo.timestamp);

 vareditComment=pageInfo.comment;
 if(!editComment){
 editComment="[No edit summary]";
 }
 editComment=editComment.replace(/'/g,"'");// HTML encode quotes

 varlastEdit="<a href='/wiki/Special:Diff/"+pageInfo.revid
 +"' title='"+editComment+"'> Last edited "
 +relativeTimestamp+" ago</a>";
 varlastEditor="<a href='/wiki/Special:Contributions/"+pageInfo.user
 +"'>"+pageInfo.user+"</a>";
 // Can be filtered if needed
 varpageTags="";
 if(pageInfo.tags.length>0){
 pageTags="<span class='mw-tag-markers'> ("+pageInfo.tags
 +")</span>";
 }

 varnoticeText=lastEdit+" by "+lastEditor+pageTags;
 varnotice="<div id='lastEdit' style='float: right;'>"+noticeText
 +"</div>"

 displayEdit(notice);
 }

 // Adapted from https://github.com/wikimedia/mediawiki-extensions-MobileFrontend/blob/master/resources/mobile.modifiedBar/time.js
 functioncalculateRelativeTimestamp(timestamp){
 // Supposedly Date parsing is a bit wonky with respect to browsers and
 // timezones, but since we have an ISO-6801 date we can risk it
 vareditTimestamp=newDate(timestamp).getTime()/1000;
 varcurrentTimestamp=Math.round(newDate().getTime()/1000);
 vartimestampDelta=currentTimestamp-editTimestamp;

 varunits=['second','minute','hour','day','month','year'],
 limits=[1,60,3600,86400,2592000,31536000];

 vari=0;
 while(i<limits.length&&timestampDelta>limits[i+1]){
 ++i;
 }
 vardelta=Math.round(timestampDelta/limits[i]);
 vardeltaUnit=units[i];
 // Pluralize units
 if(delta>1){
 deltaUnit+="s";
 }

 returndelta+" "+deltaUnit;
 }

 // Display the last edit info to the right of the site subhead
 functiondisplayEdit(notice){
 // [[MediaWiki:Gadget-metadata.js]] replaces the siteSub element so wait
 // for it to run first
 // Check if script is enabled and if it hasn't ran already
 if($("script").text().search("ext.gadget.metadata")!=-1
 &&!$(".assess-article-rating").length
 &&mw.config.get("wgNamespaceNumber")==0){
 vartarget=document.querySelector("#siteSub");
 varobserver=newMutationObserver(function(mutations){// IE 11+
 $("#siteSub").append(notice);
 observer.disconnect();
 });

 observer.observe(target,{childList:true});
 }else{
 $("#siteSub").append(notice);
 }

 // Unfortunately [[Template:Coords]] absolutely positions itself so we
 // have to move it down so we don't get obscured
 varsheet=window.document.styleSheets[0];
 sheet.insertRule('#coordinates { top: 2em !important; }',
 sheet.cssRules.length);
 }

 if(mw.config.get('skin')!="minerva"){
 $(requestRevision);
 }

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