jQuery.print

jQuery.print库,网页打印库

目前為 2018-11-07 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/38955/643037/jQueryprint.js

  1. /* @license
  2. * jQuery.print, version 1.6.0
  3. * (c) Sathvik Ponangi, Doers' Guild
  4. * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
  5. *--------------------------------------------------------------------------*/
  6. (function ($) {
  7. "use strict";
  8. // A nice closure for our definitions
  9.  
  10. function jQueryCloneWithSelectAndTextAreaValues(elmToClone, withDataAndEvents, deepWithDataAndEvents) {
  11. // Replacement jQuery clone that also clones the values in selects and textareas as jQuery doesn't for performance reasons - https://stackoverflow.com/questions/742810/clone-isnt-cloning-select-values
  12. // Based on https://github.com/spencertipping/jquery.fix.clone
  13. var $elmToClone = $(elmToClone),
  14. $result = $elmToClone.clone(withDataAndEvents, deepWithDataAndEvents),
  15. $myTextareas = $elmToClone.find('textarea').add($elmToClone.filter('textarea')),
  16. $resultTextareas = $result.find('textarea').add($result.filter('textarea')),
  17. $mySelects = $elmToClone.find('select').add($elmToClone.filter('select')),
  18. $resultSelects = $result.find('select').add($result.filter('select')),
  19. i, l, j, m;
  20.  
  21. for (i = 0, l = $myTextareas.length; i < l; ++i) {
  22. $($resultTextareas[i]).val($($myTextareas[i]).val());
  23. }
  24. for (i = 0, l = $mySelects.length; i < l; ++i) {
  25. for (j = 0, m = $mySelects[i].options.length; j < m; ++j) {
  26. if ($mySelects[i].options[j].selected === true) {
  27. $resultSelects[i].options[j].selected = true;
  28. }
  29. }
  30. }
  31. return $result;
  32. }
  33.  
  34. function getjQueryObject(string) {
  35. // Make string a vaild jQuery thing
  36. var jqObj = $("");
  37. try {
  38. jqObj = jQueryCloneWithSelectAndTextAreaValues(string);
  39. } catch (e) {
  40. jqObj = $("<span />")
  41. .html(string);
  42. }
  43. return jqObj;
  44. }
  45.  
  46. function printFrame(frameWindow, content, options) {
  47. // Print the selected window/iframe
  48. var def = $.Deferred();
  49. try {
  50. frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow;
  51. var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow;
  52. if(options.doctype) {
  53. wdoc.write(options.doctype);
  54. }
  55. wdoc.write(content);
  56. wdoc.close();
  57. var printed = false,
  58. callPrint = function () {
  59. if(printed) {
  60. return;
  61. }
  62. // Fix for IE : Allow it to render the iframe
  63. frameWindow.focus();
  64. try {
  65. // Fix for IE11 - printng the whole page instead of the iframe content
  66. if (!frameWindow.document.execCommand('print', false, null)) {
  67. // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891
  68. frameWindow.print();
  69. }
  70. // focus body as it is losing focus in iPad and content not getting printed
  71. $('body').focus();
  72. } catch (e) {
  73. frameWindow.print();
  74. }
  75. frameWindow.close();
  76. printed = true;
  77. def.resolve();
  78. };
  79. // Print once the frame window loads - seems to work for the new-window option but unreliable for the iframe
  80. $(frameWindow).on("load", callPrint);
  81. // Fallback to printing directly if the frame doesn't fire the load event for whatever reason
  82. setTimeout(callPrint, options.timeout);
  83. } catch (err) {
  84. def.reject(err);
  85. }
  86. return def;
  87. }
  88.  
  89. function printContentInIFrame(content, options) {
  90. var $iframe = $(options.iframe + "");
  91. var iframeCount = $iframe.length;
  92. if (iframeCount === 0) {
  93. // Create a new iFrame if none is given
  94. $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
  95. .prependTo('body')
  96. .css({
  97. "position": "absolute",
  98. "top": -999,
  99. "left": -999
  100. });
  101. }
  102. var frameWindow = $iframe.get(0);
  103. return printFrame(frameWindow, content, options)
  104. .done(function () {
  105. // Success
  106. setTimeout(function () {
  107. // Wait for IE
  108. if (iframeCount === 0) {
  109. // Destroy the iframe if created here
  110. $iframe.remove();
  111. }
  112. }, 1000);
  113. })
  114. .fail(function (err) {
  115. // Use the pop-up method if iframe fails for some reason
  116. console.error("Failed to print from iframe", err);
  117. printContentInNewWindow(content, options);
  118. })
  119. .always(function () {
  120. try {
  121. options.deferred.resolve();
  122. } catch (err) {
  123. console.warn('Error notifying deferred', err);
  124. }
  125. });
  126. }
  127.  
  128. function printContentInNewWindow(content, options) {
  129. // Open a new window and print selected content
  130. var frameWindow = window.open();
  131. return printFrame(frameWindow, content, options)
  132. .always(function () {
  133. try {
  134. options.deferred.resolve();
  135. } catch (err) {
  136. console.warn('Error notifying deferred', err);
  137. }
  138. });
  139. }
  140.  
  141. function isNode(o) {
  142. /* http://stackoverflow.com/a/384380/937891 */
  143. return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
  144. }
  145. $.print = $.fn.print = function () {
  146. // Print a given set of elements
  147. var options, $this, self = this;
  148. // console.log("Printing", this, arguments);
  149. if (self instanceof $) {
  150. // Get the node if it is a jQuery object
  151. self = self.get(0);
  152. }
  153. if (isNode(self)) {
  154. // If `this` is a HTML element, i.e. for
  155. // $(selector).print()
  156. $this = $(self);
  157. if (arguments.length > 0) {
  158. options = arguments[0];
  159. }
  160. } else {
  161. if (arguments.length > 0) {
  162. // $.print(selector,options)
  163. $this = $(arguments[0]);
  164. if (isNode($this[0])) {
  165. if (arguments.length > 1) {
  166. options = arguments[1];
  167. }
  168. } else {
  169. // $.print(options)
  170. options = arguments[0];
  171. $this = $("html");
  172. }
  173. } else {
  174. // $.print()
  175. $this = $("html");
  176. }
  177. }
  178. // Default options
  179. var defaults = {
  180. globalStyles: true,
  181. mediaPrint: false,
  182. stylesheet: null,
  183. noPrintSelector: ".no-print",
  184. iframe: true,
  185. append: null,
  186. prepend: null,
  187. manuallyCopyFormValues: true,
  188. deferred: $.Deferred(),
  189. timeout: 750,
  190. title: null,
  191. doctype: '<!doctype html>'
  192. };
  193. // Merge with user-options
  194. options = $.extend({}, defaults, (options || {}));
  195. var $styles = $("");
  196. if (options.globalStyles) {
  197. // Apply the stlyes from the current sheet to the printed page
  198. $styles = $("style, link, meta, base, title");
  199. } else if (options.mediaPrint) {
  200. // Apply the media-print stylesheet
  201. $styles = $("link[media=print]");
  202. }
  203. if (options.stylesheet) {
  204. // Add a custom stylesheet if given
  205. $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
  206. }
  207. // Create a copy of the element to print
  208. var copy = jQueryCloneWithSelectAndTextAreaValues($this);
  209. // Wrap it in a span to get the HTML markup string
  210. copy = $("<span/>")
  211. .append(copy);
  212. // Remove unwanted elements
  213. copy.find(options.noPrintSelector)
  214. .remove();
  215. // Add in the styles
  216. copy.append(jQueryCloneWithSelectAndTextAreaValues($styles));
  217. // Update title
  218. if (options.title) {
  219. var title = $("title", copy);
  220. if (title.length === 0) {
  221. title = $("<title />");
  222. copy.append(title);
  223. }
  224. title.text(options.title);
  225. }
  226. // Appedned content
  227. copy.append(getjQueryObject(options.append));
  228. // Prepended content
  229. copy.prepend(getjQueryObject(options.prepend));
  230. if (options.manuallyCopyFormValues) {
  231. // Manually copy form values into the HTML for printing user-modified input fields
  232. // http://stackoverflow.com/a/26707753
  233. copy.find("input")
  234. .each(function () {
  235. var $field = $(this);
  236. if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
  237. if ($field.prop("checked")) {
  238. $field.attr("checked", "checked");
  239. }
  240. } else {
  241. $field.attr("value", $field.val());
  242. }
  243. });
  244. copy.find("select").each(function () {
  245. var $field = $(this);
  246. $field.find(":selected").attr("selected", "selected");
  247. });
  248. copy.find("textarea").each(function () {
  249. // Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589
  250. var $field = $(this);
  251. $field.text($field.val());
  252. });
  253. }
  254. // Get the HTML markup string
  255. var content = copy.html();
  256. // Notify with generated markup & cloned elements - useful for logging, etc
  257. try {
  258. options.deferred.notify('generated_markup', content, copy);
  259. } catch (err) {
  260. console.warn('Error notifying deferred', err);
  261. }
  262. // Destroy the copy
  263. copy.remove();
  264. if (options.iframe) {
  265. // Use an iframe for printing
  266. try {
  267. printContentInIFrame(content, options);
  268. } catch (e) {
  269. // Use the pop-up method if iframe fails for some reason
  270. console.error("Failed to print from iframe", e.stack, e.message);
  271. printContentInNewWindow(content, options);
  272. }
  273. } else {
  274. // Use a new window for printing
  275. printContentInNewWindow(content, options);
  276. }
  277. return this;
  278. };
  279. })(jQuery);