我們的公司wiki是Mediawiki。我沒有問題把iframe放到我的網站去參考wiki上的一些文章。如何將MediaWiki頁面內容嵌入到我的網站中?
但我自己的網站有很多小部件和自己的風格。我不想包含Mediawiki導航/搜索/登錄小部件,徽標圖像。
是否有可能以及如何獲得沒有小部件(僅文章正文)的Mediawiki頁面內容?
我們的公司wiki是Mediawiki。我沒有問題把iframe放到我的網站去參考wiki上的一些文章。如何將MediaWiki頁面內容嵌入到我的網站中?
但我自己的網站有很多小部件和自己的風格。我不想包含Mediawiki導航/搜索/登錄小部件,徽標圖像。
是否有可能以及如何獲得沒有小部件(僅文章正文)的Mediawiki頁面內容?
是的。您可能需要使用action=render
url參數,例如:http://en.wikipedia.org/w/index.php?action=render&title=Main_Page。請注意,wiki中的樣式表不包含在內,因此您需要將相關規則複製到您網站的css文件中。另見this。
謝謝waldir回答!
問的問題後,我進行自己的研究,並與代碼結尾:
window.onload = function() {
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState !== 4) {
console.log("Not ready, code: %o", httpRequest.readyState);
return;
}
if (httpRequest.status !== 200) {
console.log("Server error: %o", httpRequest.status);
return;
}
var json = JSON.parse(httpRequest.responseText);
console.log("json: %o", json);
var wiki = json.query.pages["1"].revisions[0]["*"];
console.log("wiki: %o", wiki);
var html = InstaView.convert(wiki);
console.log("html: %o", html);
document.getElementById('area').innerHTML = html;
};
var url = 'https://wiki.evil-company.com/api.php?action=query&prop=revisions&format=json&titles=Main_page&rvprop=timestamp|user|comment|content';
httpRequest.open('GET', url, true);
httpRequest.send(null);
}
這裏我用https://github.com/cscott/instaview/blob/master/main.js項目,該項目增強了http://en.wikipedia.org/wiki/User:Pilaf改造JSON輸出到在瀏覽器端的HTML。
此代碼的原因,因爲我們的wiki是舊的或配置不當,並且action = render不可用。但我陷入跨域腳本問題,所以我認爲iframe與action = render是更好的解決方案。
也How do you grab an article including the links in a usable format?
看到另一個建議,使用行動=解析(http://en.wikipedia.org/w/api.php?action=parse&title=Linux)導致警告:
You are looking at the HTML representation of the XML format.
HTML is good for debugging, but is unsuitable for application use.
Specify the format parameter to change the output format.
UPDATE
完美解決方案只是追加查詢行動=渲染可以轉換成任何有效的Wiki頁面訪問量:
您可以繞過使用[JSONP(http://en.wikipedia.org/wiki/JSONP)的跨域問題,這MediaWiki的API [支持](HTTP:/ /www.mediawiki.org/wiki/API:Cross-site_requests)通過'callback'參數。至於'action = parse',你仍然需要[指定你想要的格式](http://www.mediawiki.org/wiki/API:Cross-site_requests),例如'format = json'。 –
另外,爲了確保,你知道'action = render'不是一個有效的'api.php'動作,而應該作爲[一個參數傳遞給'index.php'](http:// www .mediawiki.org/wiki/Manual:Actions),對不對? (是的,有些混淆的是,這兩個腳本都使用了一個具有相同名稱和類似用途的參數,但允許使用不同的值。) –
@IlmariKaronen非常感謝**?action = render **!我真的不知道。我工作完美,只需添加到任何有效的維基頁面的末尾! – gavenkoa