jQuery.print

jQuery.print库,网页打印库

目前為 2021-06-08 提交的版本,檢視 最新版本

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

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