2014-09-18 13 views
6

Tôi muốn thêm tập lệnh bên ngoài vào phần đầu cho tất cả các trang trong mediawiki.Làm cách nào để thêm phần bên ngoài <script> vào <head> cho tất cả các trang mediawiki?

Chức năng onBeforePageDisplay gọi lại từ BeforePageDisplay móc:

//LocalSettings.php 
... 
# Assign my functions to hook 

$wgHooks['BeforePageDisplay'][] ='onBeforePageDisplay'; 

function onBeforePageDisplay(OutputPage &$out, Skin &$skin) 
{ 
    mw.loader.load('http://static.wowhead.com/widgets/power.js', 'text/javascript'); 
    $out->addModules('mw.loader'); 
    return true; 
}; 

Trong chức năng này tôi muốn thêm

<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script> 
<script>var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": true }</script> 

để <head> phần dành cho tất cả các trang trong wiki.

Đối với các phiên bản cũ của MediaWiki dùng addScript phương pháp OutputPage đối tượng:

$out->addScript($html) 
// Add a JS file. $html is a full script tag: '<script type="text/javascript" src="..."></script>' 

nhưng bây giờ

Đối với MediaWiki 1,17 trở lên, sử dụng module ResourceLoader.

$ out-> addModules (mảng (/ modules /));

Tôi không thể làm việc đó và không tìm thấy bất kỳ ví dụ nào về điều này.

ResourceLoader description

Default_modules description

Có lẽ tôi phải sử dụng mw.loader.load module, nhưng tôi không có ý tưởng làm thế nào để làm điều đó. Giúp tôi, xin vui lòng, và xin lỗi vì tiếng anh của tôi.

P.s. this giải pháp làm việc, nhưng không đúng. Cần giải pháp với ResourseLoader đã sử dụng. (C) IMHO

+0

Không có cách nào để bạn có thể tải tập lệnh bên ngoài bằng ResourseLoader. Tôi đã viết phần mở rộng của riêng mình để làm điều đó. Giải pháp sử dụng tốt hơn từ câu trả lời của tôi. И у тебя в коде яваскрипт вставлен в пхп. :) – hlcs

Trả lời

6

Giải pháp rất đơn giản (có vẻ như giải pháp 2nd):

//LocalSettings.php 
... 
# Assign my functions to hook 

$wgHooks['BeforePageDisplay'][] ='onBeforePageDisplay'; 

function onBeforePageDisplay(OutputPage &$out, Skin &$skin) 
{ 
    $script = '<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script><script>var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": true }</script>'; 
    $out->addHeadItem("wowhead script", $script); 
    return true; 
}; 

Bằng cách này trông đẹp hơn sau đó this, bởi vì nó làm việc với OutputPage trực tiếp (sau khi phân tích cú pháp).

+0

Điều này dẫn đến hiệu suất kém. Bạn nên sử dụng [ResourceLoader] (https://www.mediawiki.org/wiki/ResourceLoader/Developing_with_ResourceLoader). – Tgr

+0

Đã làm việc tuyệt vời. Không có tác động hiệu suất đáng chú ý. Cảm ơn! –

Các vấn đề liên quan