完成谷歌广告功能

master
落雨楓 2 years ago
parent 246d1a7c3d
commit e45688a629

@ -3,6 +3,7 @@
namespace Isekai; namespace Isekai;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use Config;
use OutputPage; use OutputPage;
use Html; use Html;
use Title; use Title;
@ -78,6 +79,75 @@ var _hmt = _hmt || [];
</script> </script>
HTML HTML
); );
self::showAds($outputPage);
}
public static function showAds(OutputPage $outputPage) {
global $wgIsekaiShowAds;
$service = MediaWikiServices::getInstance();
$isView = $outputPage->getContext()->getRequest()->getText( 'action', 'view' ) == 'view';
$showAdsSidebar = $service->getUserOptionsLookup()->getOption($outputPage->getUser(), 'isekai-show-ads-sidebar');
$showAdsBottom = $service->getUserOptionsLookup()->getOption($outputPage->getUser(), 'isekai-show-ads-bottom');
if($wgIsekaiShowAds
&& ($showAdsSidebar || $showAdsBottom)
&& $outputPage->getTitle()->getNamespace() !== NS_SPECIAL
&& $isView){
// 检测Adblock
$outputPage->addHeadItem('adblock-detect', '<script src="/peel1.js"></script>');
// 谷歌广告
$outputPage->addHeadItem(
'googlead',
<<<HTML
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8607817613161226" crossorigin="anonymous"></script>
HTML
);
if ($showAdsSidebar && $outputPage->getSkin()->getSkinName() === 'timeless') {
$outputPage->addHTML(
<<<HTML
<script type="text/javascript">
if (window.innerWidth >= 1100) {
var adElem = document.createElement('div');
adElem.className = 'googlead-container googlead-container-sidebar';
adElem.innerHTML = '<ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8607817613161226" data-ad-slot="8245595332" data-ad-format="rectangle" data-full-width-responsive="true"></ins>';
var relatedNav = document.querySelector('#mw-related-navigation');
if (relatedNav) {
relatedNav.appendChild(adElem);
(adsbygoogle = window.adsbygoogle || []).push({});
}
}
</script>
HTML
);
}
if ($showAdsBottom) {
$outputPage->addHTML(
<<<HTML
<div class="googlead-container googlead-container-bottom">
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-8607817613161226"
data-ad-slot="3245545943"
data-ad-format="horizontal"
data-full-width-responsive="true"></ins>
</div>
HTML
);
$outputPage->addHTML(
<<<HTML
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
HTML
);
}
}
} }
public static function onUserPage(OutputPage $outputPage) { public static function onUserPage(OutputPage $outputPage) {
@ -177,4 +247,22 @@ HTML
} }
return true; return true;
} }
public static function onResourceLoaderGetConfigVars(array &$vars, string $skin, Config $config){
$vars['wgIsekaiShowAds'] = $config->get('IsekaiShowAds');
}
public static function onGetPreferences(User $user, array &$preferences){
$preferences['isekai-show-ads-bottom'] = [
'type' => 'toggle',
'label-message' => 'isekai-show-ads-bottom',
'section' => 'misc/isekai-ads',
];
$preferences['isekai-show-ads-sidebar'] = [
'type' => 'toggle',
'label-message' => 'isekai-show-ads-sidebar',
'section' => 'misc/isekai-ads',
];
}
} }

@ -11,7 +11,7 @@
}, },
"MessagesDirs": { "MessagesDirs": {
"Isekai": [ "IsekaiMisc": [
"i18n" "i18n"
] ]
}, },
@ -37,6 +37,12 @@
"HtmlPageLinkRendererBegin": [ "HtmlPageLinkRendererBegin": [
"Isekai\\Isekai::onHtmlPageLinkRendererBegin" "Isekai\\Isekai::onHtmlPageLinkRendererBegin"
], ],
"ResourceLoaderGetConfigVars": [
"Isekai\\Isekai::onResourceLoaderGetConfigVars"
],
"GetPreferences": [
"Isekai\\Isekai::onGetPreferences"
],
"EditPage::attemptSave": [ "EditPage::attemptSave": [
"Isekai\\Isekai::onAttemptSave" "Isekai\\Isekai::onAttemptSave"
] ]
@ -63,6 +69,9 @@
"isekai-login-prompt-btn-login", "isekai-login-prompt-btn-login",
"isekai-login-prompt-btn-createaccount", "isekai-login-prompt-btn-createaccount",
"isekai-login-prompt-btn-cancel", "isekai-login-prompt-btn-cancel",
"isekai-ads-prompt-title",
"isekai-ads-prompt-content",
"isekai-ads-prompt-btn-cancel",
"nav-login-createaccount" "nav-login-createaccount"
] ]
}, },
@ -100,7 +109,14 @@
}, },
"ResourceFileModulePaths": { "ResourceFileModulePaths": {
"localBasePath": "modules", "localBasePath": "modules",
"remoteExtPath": "Isekai/modules" "remoteExtPath": "IsekaiMisc/modules"
},
"DefaultUserOptions": {
"isekai-show-ads-bottom": true,
"isekai-show-ads-sidebar": true
},
"config": {
"IsekaiShowAds": false
}, },
"manifest_version": 1 "manifest_version": 1
} }

@ -8,5 +8,27 @@
"isekai-login-prompt-content": "Share your imagine world, contribute your fictional settings, and help more people! now, let's join the Fantasy World wiki.", "isekai-login-prompt-content": "Share your imagine world, contribute your fictional settings, and help more people! now, let's join the Fantasy World wiki.",
"isekai-login-prompt-btn-login": "Login", "isekai-login-prompt-btn-login": "Login",
"isekai-login-prompt-btn-createaccount": "Create Account", "isekai-login-prompt-btn-createaccount": "Create Account",
"isekai-login-prompt-btn-cancel": "Cancel" "isekai-login-prompt-btn-cancel": "Cancel",
"isekai-background-param-error": "Background image parameter error: ",
"isekai-editor-prompt-content": "Please select the editor, use the Standard Editor will get a better writing experience.",
"isekai-editor-prompt-btn-cancel": "Cancel",
"isekai-editor-prompt-btn-use-mobile": "Mobile Editor",
"isekai-editor-prompt-btn-use-desktop": "Standard Editor",
"isekai-userpage": "User",
"isekai-chart-undefined-type": "Undefined chart type",
"isekai-chart-unknow-type": "Unknow chart type: {0}",
"isekai-chart-transform-error": "Data conversion errors: {0}",
"isekai-chart-default-row-name": "Data {0}",
"prefs-isekai-ads": "Support Fanta World Wiki",
"isekai-show-ads-bottom": "Display ads at the bottom of the content page",
"isekai-show-ads-sidebar": "Display ads in the sidebar of the PC version",
"isekai-ads-prompt-title": "Please allow ads on Fanta World Wiki",
"isekai-ads-prompt-content": "<p>Looks like you're using an ad blocker.</p><p>Fanta World Wiki rely on advertising to help fund our site.</p><p>If you want to support Fanta World Wiki, you can turn off Adblock, Adblock Pro, and other plugins on this site.</p>",
"isekai-ads-prompt-btn-cancel": "No longer display"
} }

@ -9,6 +9,7 @@
"isekai-login-prompt-btn-login": "登录", "isekai-login-prompt-btn-login": "登录",
"isekai-login-prompt-btn-createaccount": "创建账户", "isekai-login-prompt-btn-createaccount": "创建账户",
"isekai-login-prompt-btn-cancel": "残忍拒绝", "isekai-login-prompt-btn-cancel": "残忍拒绝",
"isekai-background-param-error": "背景图片参数错误:", "isekai-background-param-error": "背景图片参数错误:",
"isekai-editor-prompt-content": "请选择编辑器,使用完整版编辑器可以获得更好的体验", "isekai-editor-prompt-content": "请选择编辑器,使用完整版编辑器可以获得更好的体验",
@ -21,5 +22,13 @@
"isekai-chart-undefined-type": "未定义图表类型", "isekai-chart-undefined-type": "未定义图表类型",
"isekai-chart-unknow-type": "未知的图表类型:{0}", "isekai-chart-unknow-type": "未知的图表类型:{0}",
"isekai-chart-transform-error": "数据转换错误:{0}", "isekai-chart-transform-error": "数据转换错误:{0}",
"isekai-chart-default-row-name": "数据 {0}" "isekai-chart-default-row-name": "数据 {0}",
"prefs-isekai-ads": "支持异世界百科",
"isekai-show-ads-bottom": "在页面底部显示广告",
"isekai-show-ads-sidebar": "在PC版侧栏显示广告",
"isekai-ads-prompt-title": "您似乎屏蔽了广告",
"isekai-ads-prompt-content": "<p>目前,页面广告是异世界百科的唯一收入来源。</p><p>如果您想要支持异世界百科可以在此站点上关闭Adblock、Adblock Pro等插件。</p><p>如果想要移除广告空位,可以在用户-参数设置中关闭广告。</p>",
"isekai-ads-prompt-btn-cancel": "不再显示"
} }

@ -9,6 +9,7 @@
"isekai-login-prompt-btn-login": "登錄", "isekai-login-prompt-btn-login": "登錄",
"isekai-login-prompt-btn-createaccount": "創建賬號", "isekai-login-prompt-btn-createaccount": "創建賬號",
"isekai-login-prompt-btn-cancel": "關閉", "isekai-login-prompt-btn-cancel": "關閉",
"isekai-background-param-error": "背景圖片參數錯誤:", "isekai-background-param-error": "背景圖片參數錯誤:",
"isekai-editor-prompt-content": "請選擇編輯器,使用完整版編輯器可以獲得更好的體驗", "isekai-editor-prompt-content": "請選擇編輯器,使用完整版編輯器可以獲得更好的體驗",
@ -21,5 +22,13 @@
"isekai-chart-undefined-type": "未定義圖表類型", "isekai-chart-undefined-type": "未定義圖表類型",
"isekai-chart-unknow-type": "未知的圖表類型:{0}", "isekai-chart-unknow-type": "未知的圖表類型:{0}",
"isekai-chart-transform-error": "資料轉換錯誤:{0}", "isekai-chart-transform-error": "資料轉換錯誤:{0}",
"isekai-chart-default-row-name": "資料 {0}" "isekai-chart-default-row-name": "資料 {0}",
"prefs-isekai-ads": "支持異世界百科",
"isekai-show-ads-bottom": "在頁面底部顯示廣告",
"isekai-show-ads-sidebar": "在PC版側欄顯示廣告",
"isekai-ads-prompt-title": "您似乎屏蔽了廣告",
"isekai-ads-prompt-content": "<p>目前,頁面廣告是異世界百科的唯一收入來源。</p><p>如果您想要支持異世界百科可以在此網站上關閉Adblock、Adblock Pro等擴展程式。</p><p>如果想要移除廣告空位,可以在用戶-參數設置中關閉廣告。</p>",
"isekai-ads-prompt-btn-cancel": "不再顯示"
} }

@ -57,14 +57,14 @@ function getPageVisited(){
} }
} }
function setDismiss(){ function setDismissLoginPrompt(){
var time = Date.now() / 1000; var time = Date.now() / 1000;
localStorage.setItem('dismissLoginPrompt', time.toFixed(0)); localStorage.setItem('dismissLoginPrompt', time.toFixed(0));
} }
function shouldShowPrompt(){ function shouldShowLoginPrompt(){
if(!mw.user.isAnon()){ //用户已登录 if(!mw.user.isAnon()){ //用户已登录
setDismiss(); setDismissLoginPrompt();
return false; return false;
} }
@ -90,6 +90,29 @@ function shouldShowPrompt(){
return true; return true;
} }
function setDismissAdsPrompt(){
var time = Date.now() / 1000;
localStorage.setItem('dismissAdsPrompt', time.toFixed(0));
}
function shouldShowAdsPrompt(){
if(mw.config.get('wgCanonicalNamespace') === 'Special' && mw.config.get('wgAction') === 'view'){ // 仅查看页面弹出
return false;
}
var time = Date.now() / 1000;
var expire = 86400 * 7; //7天内不提醒
var dismissTime = localStorage.getItem('dismissAdsPrompt');
if(dismissTime !== null){
dismissTime = parseInt(dismissTime);
if(dismissTime + expire > time){
return false;
}
}
return true;
}
$(function(){ $(function(){
//锚链接偏移 //锚链接偏移
$('body').on('click', 'a', function() { $('body').on('click', 'a', function() {
@ -122,6 +145,8 @@ $(function(){
// 初始化dialog // 初始化dialog
var messageDialog = new OO.ui.MessageDialog(); var messageDialog = new OO.ui.MessageDialog();
var windowManager = new OO.ui.WindowManager(); var windowManager = new OO.ui.WindowManager();
window._isekaiMessageDialog = messageDialog;
window._isekaiWindowManager = windowManager;
$('body').append(windowManager.$element); $('body').append(windowManager.$element);
windowManager.addWindows([messageDialog]); windowManager.addWindows([messageDialog]);
@ -164,6 +189,7 @@ $(function(){
} }
//显示登录引导框 //显示登录引导框
if (shouldShowLoginPrompt()) {
var cancelBtn = new OO.ui.ActionWidget({ var cancelBtn = new OO.ui.ActionWidget({
action: 'cancel', action: 'cancel',
label: mw.message('isekai-login-prompt-btn-cancel').text(), label: mw.message('isekai-login-prompt-btn-cancel').text(),
@ -183,7 +209,7 @@ $(function(){
}); });
cancelBtn.on('click', function(){ cancelBtn.on('click', function(){
setDismiss(); setDismissLoginPrompt();
}); });
registerBtn.on('click', function(){ registerBtn.on('click', function(){
@ -194,13 +220,71 @@ $(function(){
location.href = mw.util.getUrl('Special:Userlogin', {returnto: mw.config.get('wgPageName')}); location.href = mw.util.getUrl('Special:Userlogin', {returnto: mw.config.get('wgPageName')});
}); });
if(shouldShowPrompt()){
windowManager.openWindow(messageDialog, { windowManager.openWindow(messageDialog, {
message: mw.message('isekai-login-prompt-content').parse(), message: mw.message('isekai-login-prompt-content').parse(),
actions: [cancelBtn, loginBtn], // 去除注册按钮 actions: [cancelBtn, loginBtn], // 去除注册按钮
}); });
} }
// 广告存活检测
if (mw.config.get('wgIsekaiShowAds')) {
setTimeout(function() {
if ($('#ad-dec-7kVsyacCEKVGBLOiLVQ').length === 0) {
if (shouldShowAdsPrompt()) {
//显示广告引导框
var cancelBtn = new OO.ui.ActionWidget({
action: 'cancel',
label: mw.message('isekai-ads-prompt-btn-cancel').text(),
active: true,
flags: ['safe', 'close'],
});
cancelBtn.on('click', function(){
setDismissAdsPrompt();
});
windowManager.openWindow(messageDialog, {
title: mw.message('isekai-ads-prompt-title').parse(),
message: '',
size: 'medium',
actions: [cancelBtn],
});
messageDialog.$body.find('> .oo-ui-messageDialog-container > .oo-ui-messageDialog-text > .oo-ui-messageDialog-message')
.append(mw.message('isekai-ads-prompt-content').parse());
}
}
}, 500);
}
/* GoogleAdblock
if (mw.config.get('wgIsekaiShowAds')) {
window.googlefc = window.googlefc || {};
window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];
googlefc.controlledMessagingFunction = (message) => {
if (shouldShowAdsPrompt()) {
message.proceed(true);
} else {
message.proceed(false);
}
};
function signalGooglefcPresent() {
if (!window.frames['googlefcPresent']) {
if (document.body) {
const iframe = document.createElement('iframe');
iframe.style = 'width: 0; height: 0; border: none; z-index: -1000; left: -1000px; top: -1000px;';
iframe.style.display = 'none';
iframe.name = 'googlefcPresent';
document.body.appendChild(iframe);
setDismissAdsPrompt();
} else {
window.requestAnimationFrame(signalGooglefcPresent);
}
}
}
signalGooglefcPresent();
}*/
// 显示用户头像 // 显示用户头像
if(mw.config.get('wgNamespaceNumber') === 2 && mw.config.get('wgAvatarTemplate')) { if(mw.config.get('wgNamespaceNumber') === 2 && mw.config.get('wgAvatarTemplate')) {
$('<div id="userHeading" class="user-heading"></div>').insertAfter("#firstHeading"); $('<div id="userHeading" class="user-heading"></div>').insertAfter("#firstHeading");
@ -213,6 +297,7 @@ $(function(){
userHeading.append(`<img src="${avatarUrl}" alt="${avatarAlt}" class="user-avatar">`); userHeading.append(`<img src="${avatarUrl}" alt="${avatarAlt}" class="user-avatar">`);
} }
// 更改ContentTranslation的机器翻译名字
if (mw.cx && mw.cx.MachineTranslationManager) { if (mw.cx && mw.cx.MachineTranslationManager) {
let _getProviderLabel = mw.cx.MachineTranslationManager.prototype.getProviderLabel; let _getProviderLabel = mw.cx.MachineTranslationManager.prototype.getProviderLabel;
mw.cx.MachineTranslationManager.prototype.getProviderLabel = function ( provider ) { mw.cx.MachineTranslationManager.prototype.getProviderLabel = function ( provider ) {

@ -271,3 +271,24 @@ html.content-loaded body.skin-timeless {
.oo-ui-messageDialog-actions-horizontal .oo-ui-actionWidget { .oo-ui-messageDialog-actions-horizontal .oo-ui-actionWidget {
width: auto !important; width: auto !important;
} }
.googlead-container-bottom {
margin-top: 10px;
margin-bottom: 8px;
width: 100%;
min-height: 90px;
}
.skin-minerva .googlead-container-bottom {
margin-top: 30px;
margin-left: 16px;
margin-bottom: 16px;
margin-right: 16px;
}
@media screen and (max-width: 1099px) {
.googlead-container-sidebar {
display: none;
}
}
Loading…
Cancel
Save