如何通过破坏我的MediaWiki userScript替换var

问题描述 投票:0回答:1

我试图理解适应MediaWiki用户脚本的basic example的过程。

它以原始形式工作,但是当我在var功能中将let替换为format_words时,它将停止工作。我对JavaScript和jQuery很陌生。这是工作版本:

function format_words(title, numWords){
  var text = title+" ("+numWords+" words)";
  return text;
}

$(document).ready(function(){
  var numWords = $("#mw-content-text > div").text().split(" ").length;
  var header = $("#firstHeading").text();
  $("#firstHeading").text(format_words(header, numWords));
});

这里是无效的版本

function format_words(title, numWords){
  let text = title+" ("+numWords+" words)";
  return text;
}

$(document).ready(function(){
  var numWords = $("#mw-content-text > div").text().split(" ").length;
  var header = $("#firstHeading").text();
  $("#firstHeading").text(format_words(header, numWords));
});

第一个给出此h1标题的线索:

User:Wolf / vector.js(74个字)

第二个给

User:Wolf / vector.js

我在MediaWiki 1.31.6和MediaWiki 1.35.0-wmf.34(6ff15fd)中进行了测试,效果相同。

在慢慢熟悉jQuery之后,我期望很多异步性,但是在这一点上,我无法弄清它的影响。

计数器示例

[当我在本地HTML + JavaScript中使用热jQuery测试相似的代码时,从var切换到let不会破坏代码。

控制台日志

感谢charlietfl我将脚本更改为

function format_words(title, numWords){
  console.log("format_words [enter]");
  let text = title+" ("+numWords+" words)";
  console.log("format_words: text:"+text);
  return text;
}

$(document).ready(function(){
  console.log("on ready [enter]");
  var numWords = $("#mw-content-text > div").text().split(" ").length;
  var header = $("#firstHeading").text();
  $("#firstHeading").text(format_words(header, numWords));
  console.log("on ready [leave]");
});

...并打开控制台。对于let版本,它显示以下错误:

JavaScript parse error: Parse error: Missing ; before statement in file↵
 'User:Wolf/vector.js' on line 3 vector.js line 1 > scriptElement:1:8
    <anonym> https://some.wiki/w/User:Wolf/vector.js line 1 > scriptElement:1
    jQuery 9
        DOMEval
        globalEval
        runScript
        fire
        add
        always
        checkCssHandles
        execute
        implement
    <anonym> https://some.wiki/load.php?debug=false&lang=en&modules=user&↵
skin=vector&user=Wolf&version=0hjxbj8:1

我将其读为let,JavaScript不支持。很奇怪:我确定JavaScript来自浏览器吗?它与我的Counter示例一起工作。

这里发生了什么?

javascript jquery mediawiki
1个回答
0
投票

MediaWiki使用的JS minifier不支持ES6语言功能,例如let

您看到的控制台错误是由(服务器端)minifier生成的错误,已作为脚本传输。

© www.soinside.com 2019 - 2024. All rights reserved.