Florianschmidtwelzow has uploaded a new change for review.
https://gerrit.wikimedia.org/r/272762
Change subject: Avoid using document.write
......................................................................
Avoid using document.write
document.write was stubbed in Ic9056d7a779f234a28ddad005fd4d76f2e750fcc and
has a slightly different behaviour now (it now appends the given parameters
to the body directly, instead of adding it to the position where the call
was made). This seems to be a race condition with the usage of document.write
in DismissableSiteNotice to add the site notice with JS for anonymous users and
sometimes results in an undesired outcome.
Instead of having this problem, this change replaces the usage of document.write
by ading a new module, which will add the site notice contents for anonymous
users
with jQuery into the correct position. It also populates the site notice in a
new global JS variable wgDismissableSiteNotice to accomplish the goal.
This probably fixes T125323.
Bug: T125323
Change-Id: Ideaa18f5fb468261705c115c68c454919ab22c29
---
M DismissableSiteNotice.hooks.php
M extension.json
A modules/ext.dismissableSiteNotice.anon.js
3 files changed, 44 insertions(+), 2 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DismissableSiteNotice
refs/changes/62/272762/1
diff --git a/DismissableSiteNotice.hooks.php b/DismissableSiteNotice.hooks.php
index d5b5c91..7a70221 100644
--- a/DismissableSiteNotice.hooks.php
+++ b/DismissableSiteNotice.hooks.php
@@ -2,6 +2,8 @@
class DismissableSiteNoticeHooks {
+ protected static $siteNotice = false;
+
/**
* @param string $notice
* @param Skin $skin
@@ -9,6 +11,8 @@
*/
public static function onSiteNoticeAfter( &$notice, $skin ) {
global $wgMajorSiteNoticeID, $wgDismissableSiteNoticeForAnons;
+
+ $out = $skin->getOutput();
if ( !$notice ) {
return true;
@@ -20,7 +24,6 @@
$major = (int) $wgMajorSiteNoticeID;
$minor = (int) $skin->msg( 'sitenotice_id'
)->inContentLanguage()->text();
- $out = $skin->getOutput();
$out->addModuleStyles(
'ext.dismissableSiteNotice.styles' );
$out->addModules( 'ext.dismissableSiteNotice' );
$out->addJsConfigVars( 'wgSiteNoticeId',
"$major.$minor" );
@@ -40,7 +43,29 @@
if ( $skin->getUser()->isAnon() ) {
// Hide the sitenotice from search engines (see bug
T11209 comment 4)
// XXX: Does this actually work?
- $notice = Html::inlineScript( Xml::encodeJsCall(
'document.write', array( $notice ) ) );
+ // will be added as a global JS variable and added to
the output in a JS module
+ self::$siteNotice = $notice;
+ $out->addModules( 'ext.dismissableSiteNotice.anon' );
+ // add an empty span, which will ensure, that the
siteNotice div is created, but without any visible
+ // content
+ $notice = '<span></span>';
+ }
+
+ return true;
+ }
+
+ /**
+ * MakeGlobalVariableScript hook handler.
+ *
+ * Adds the site notice (if there's any) to the global JS variable set.
MUST be run after the
+ * onSiteNoticeAfter hook handler.
+ *
+ * @param array $vars
+ * @param OutputPage $out
+ */
+ public static function onMakeGlobalVariablesScript( &$vars, OutputPage
$out ) {
+ if ( self::$siteNotice ) {
+ $vars['wgDismissableSiteNotice'] = self::$siteNotice;
}
return true;
diff --git a/extension.json b/extension.json
index 661cddb..3524c01 100644
--- a/extension.json
+++ b/extension.json
@@ -17,6 +17,9 @@
"Hooks": {
"SiteNoticeAfter": [
"DismissableSiteNoticeHooks::onSiteNoticeAfter"
+ ],
+ "MakeGlobalVariablesScript": [
+
"DismissableSiteNoticeHooks::onMakeGlobalVariablesScript"
]
},
"MessagesDirs": {
@@ -36,6 +39,13 @@
"mobile"
]
},
+ "ext.dismissableSiteNotice.anon": {
+ "scripts": "ext.dismissableSiteNotice.anon.js",
+ "targets": [
+ "desktop",
+ "mobile"
+ ]
+ },
"ext.dismissableSiteNotice.styles": {
"styles": "ext.dismissableSiteNotice.css",
"targets": [
diff --git a/modules/ext.dismissableSiteNotice.anon.js
b/modules/ext.dismissableSiteNotice.anon.js
new file mode 100644
index 0000000..79ed2ee
--- /dev/null
+++ b/modules/ext.dismissableSiteNotice.anon.js
@@ -0,0 +1,7 @@
+( function ( mw, $ ) {
+
+ if ( mw.config.get( 'wgDismissableSiteNotice' ) ) {
+ $( '#siteNotice' ).html( mw.config.get(
'wgDismissableSiteNotice' ) );
+ }
+
+}( mediaWiki, jQuery ) );
--
To view, visit https://gerrit.wikimedia.org/r/272762
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ideaa18f5fb468261705c115c68c454919ab22c29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DismissableSiteNotice
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits