4,554
edits
| No edit summary | No edit summary | ||
| Line 10: | Line 10: | ||
|      }); |      }); | ||
|      function getApiUrl() { |      function getApiUrl() { | ||
|          //  |          // With wgScriptPath = "", api.php is at root | ||
|          return '/api.php'; | |||
|     } | |||
|     // Resolve a title to a pageid (or null if missing) | |||
|     function resolveTitleToPageId(title, cb) { | |||
|          if ( |          $.getJSON(getApiUrl(), { | ||
|             action: 'query', | |||
|             prop: 'info', | |||
|             titles: title, | |||
|             format: 'json', | |||
|             formatversion: 2 | |||
|          }).done(function (res) { | |||
|             if (!res || !res.query || !res.query.pages || !res.query.pages.length) { | |||
|              console.warn('[print] query returned no pages for title:', title, res); | |||
|             cb(null); | |||
|             return; | |||
|             } | |||
|             var page = res.query.pages[0]; | |||
|             if (page.missing) { | |||
|             console.warn('[print] page is missing for title:', title); | |||
|             cb(null); | |||
|             return; | |||
|             } | |||
|             cb(page.pageid); | |||
|          }).fail(function (xhr) { | |||
|             console.warn('[print] query fail:', xhr && xhr.status, xhr && xhr.statusText); | |||
|             cb(null); | |||
|          }); | |||
|      } |      } | ||
|      // Loop through each card to format related articles |      // Loop through each card to format related articles | ||
| Line 1,070: | Line 1,080: | ||
|      // Print current entry only |      // Print current entry only | ||
|      $('#print-button').on('click', function () { |      $('#print-button').on('click', function () { | ||
|          var  |          var title = window.currentEntryTitle; | ||
|          if (! |          if (!title) { console.warn('[print] no currentEntryTitle'); window.print(); return; } | ||
|         console.log('[print] resolving title:', title); | |||
|          resolveTitleToPageId(title, function (pageid) { | |||
|             if (!pageid) { | |||
|             console.warn('[print] could not resolve title to pageid; fallback print.'); | |||
|             window.print(); | |||
|             return; | |||
|             } | |||
|             var apiUrl = getApiUrl(); | |||
|             console.log('[print] parsing pageid:', pageid, 'via', apiUrl); | |||
|             $.getJSON(apiUrl, { | |||
|              action: 'parse', |              action: 'parse', | ||
|              pageid: pageid,          // use pageid, not 'page' | |||
|              prop: 'text', |              prop: 'text', | ||
|              format: 'json', |              format: 'json', | ||
|              formatversion: 2 |              formatversion: 2 | ||
|             }).done(function (res) { | |||
|              if (!res || !res.parse || !res.parse.text) { |              if (!res || !res.parse || !res.parse.text) { | ||
|                 console.warn('[print] parse returned no text for pageid:', pageid, res); | |||
|                 window.print(); | |||
|                 return; | |||
|              } |              } | ||
|              var html = res.parse.text; |              var html = res.parse.text; | ||
|              var $tmp = $('<div>').html(html); |              var $tmp = $('<div>').html(html); | ||
| Line 1,114: | Line 1,126: | ||
|              var doc = iframe.contentDocument || iframe.contentWindow.document; |              var doc = iframe.contentDocument || iframe.contentWindow.document; | ||
|              // Raw Print.css  |              // Raw Print.css (works with wgScriptPath = "") | ||
|              var printCssUrl =  |              var printCssUrl = '/index.php?title=MediaWiki:Print.css&action=raw&ctype=text/css'; | ||
|              doc.open(); |              doc.open(); | ||
|              doc.write( |              doc.write( | ||
|                 '<!doctype html><html><head><meta charset="utf-8">' + | |||
|                 '<title>Print</title>' + | |||
|                 '<link rel="stylesheet" href="' + printCssUrl + '">' + | |||
|                 '</head><body>' + $print.prop('outerHTML') + '</body></html>' | |||
|              ); |              ); | ||
|              doc.close(); |              doc.close(); | ||
|              iframe.onload = function () { |              iframe.onload = function () { | ||
|                 try { iframe.contentWindow.focus(); iframe.contentWindow.print(); } | |||
|                 finally { setTimeout(function () { document.body.removeChild(iframe); }, 500); } | |||
|              }; |              }; | ||
|             }).fail(function (xhr) { | |||
|              console.warn('[print]  |              console.warn('[print] parse fail:', xhr && xhr.status, xhr && xhr.statusText); | ||
|              window.print(); |              window.print(); | ||
|             }); | |||
|          }); |          }); | ||
|      }); |      }); | ||