index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const Print = function (dom, options) {
  2. if (!(this instanceof Print)) return new Print(dom, options);
  3. this.options = this.extend(
  4. {
  5. noPrint: '.no-print',
  6. type: 'window',
  7. },
  8. options,
  9. );
  10. if (typeof dom === 'string') {
  11. this.dom = document.querySelector(dom);
  12. } else {
  13. this.dom = dom;
  14. }
  15. this.init();
  16. };
  17. Print.prototype = {
  18. init() {
  19. const content = this.getStyle() + this.getHtml();
  20. if (this.options.type === 'iframe') {
  21. this.writeIframe(content);
  22. } else {
  23. this.writeDocument(content);
  24. }
  25. },
  26. extend(obj, obj2) {
  27. for (const k in obj2) {
  28. obj[k] = obj2[k];
  29. }
  30. return obj;
  31. },
  32. getStyle() {
  33. let str = '';
  34. const styles = document.querySelectorAll('style,link');
  35. for (let i = 0; i < styles.length; i++) {
  36. str += styles[i].outerHTML;
  37. }
  38. str +=
  39. `<style>${
  40. this.options.noPrint ? this.options.noPrint : '.no-print'
  41. }{display:none;}</style>`;
  42. return str;
  43. },
  44. getHtml() {
  45. const inputs = document.querySelectorAll('input');
  46. const textareas = document.querySelectorAll('textarea');
  47. const selects = document.querySelectorAll('select');
  48. for (const k in inputs) {
  49. if (inputs[k].type === 'checkbox' || inputs[k].type === 'radio') {
  50. if (inputs[k].checked === true) {
  51. inputs[k].setAttribute('checked', 'checked');
  52. } else {
  53. inputs[k].removeAttribute('checked');
  54. }
  55. } else if (inputs[k].type === 'text') {
  56. inputs[k].setAttribute('value', inputs[k].value);
  57. }
  58. }
  59. for (const k2 in textareas) {
  60. if (textareas[k2].type === 'textarea') {
  61. textareas[k2].innerHTML = textareas[k2].value;
  62. }
  63. }
  64. for (const k3 in selects) {
  65. if (selects[k3].type === 'select-one') {
  66. const child = selects[k3].children;
  67. for (const i in child) {
  68. if (child[i].tagName === 'OPTION') {
  69. if (child[i].selected == true) {
  70. child[i].setAttribute('selected', 'selected');
  71. } else {
  72. child[i].removeAttribute('selected');
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return this.dom.outerHTML;
  79. },
  80. writeIframe(content) {
  81. const iframe = document.createElement('iframe');
  82. const f = document.body.appendChild(iframe);
  83. const w = f.contentWindow || f.contentDocument;
  84. const doc = f.contentDocument || f.contentWindow.document;
  85. iframe.id = 'myIframe';
  86. iframe.style = 'position:absolute;width:0;height:0;top:-10px;left:-10px;';
  87. doc.open();
  88. doc.write(content);
  89. doc.close();
  90. alert('这是一个浏览器提示窗口!');
  91. // this.toPrint(w);
  92. document.body.removeChild(iframe);
  93. },
  94. writeDocument(content) {
  95. const newWindow = window.open('_blank');
  96. newWindow.document.write(content);
  97. newWindow.document.close();
  98. // this.toPrint(newWindow);
  99. // setTimeout(() => {
  100. // newWindow.close();
  101. // }, 100);
  102. },
  103. toPrint(frameWindow) {
  104. try {
  105. setTimeout(() => {
  106. frameWindow.focus();
  107. try {
  108. if (!frameWindow.document.execCommand('print', false, null)) {
  109. frameWindow.print();
  110. }
  111. } catch (e) {
  112. frameWindow.print();
  113. }
  114. frameWindow.close();
  115. }, 100);
  116. } catch (err) {
  117. console.log(err);
  118. }
  119. },
  120. };
  121. module.exports = Print;