如何在Chrome中访问打印窗口的HTML?

huangapple go评论70阅读模式
英文:

How to access the HTML of the print window in Chrome?

问题

I want to write code that prints a web page. print() is not enough because it only opens the browser's print window, and does not print immediately. In addition, I am also interested in controlling the printing options (such as printing to PDF or a certain size).

So I opened Chrome's developer tools with the print window open, copied the js path of the save button, and tried to write the following code in a web page I wanted to print:

print()
document.querySelector("body > print-preview-app").shadowRoot.querySelector("#sidebar").shadowRoot.querySelector("print-preview-button-strip").shadowRoot.querySelector("div > cr-button .action-button").click()

I got an error because this element does not appear on the page itself but in the print window. It turns out that the window is actually a separate page (chrome://print/), but in some way it is embedded in the printed page or it appears unrelated to it.

My question is, how can I run JS in such a case on the window?

My question is definitely different from this question. There the question was about a plugin, and the problem was accessing the chrome:// pages because they are like that, and the browser does not allow accessing them. But I'm writing a regular script (not a plugin) and as such it can always access such pages. The problem in my case is that the print window is not part of the website page but is displayed separately.

I will clarify again. The problem I encountered is with accessing the HTML of a page other than the current page. Using a plugin, it should actually be technically possible, but the browsers block it. So the question of how to do this with a plugin is different from the question of how to do it with a regular script.

post Scriptum. 1. Perhaps it is also possible to make direct use of JS through which Chrome prints, but at the moment it is not relevant for me. The question is how to access the HTML elements as above.

  1. I have seen similar questions (like this one). I didn't see any answers.
英文:

I want to write code that prints a web page. print() is not enough because it only opens the browser's print window, and does not print immediately. In addition, I am also interested in controlling the printing options (such as printing to PDF or a certain size).

So I opened Chrome's developer tools with the print window open, copied the js path of the save button, and tried to write the following code in a web page I wanted to print:

print()
document.querySelector("body > print-preview-app").shadowRoot.querySelector("#sidebar").shadowRoot.querySelector("print-preview-button-strip").shadowRoot.querySelector("div > cr-button .action-button")click()

I got an error because this element does not appear on the page itself but in the print window. It turns out that the window is actually a separate page (chrome://print/), but in some way it is embedded in the printed page or it appears unrelated to it.

My question is, how can I run JS in such a case on the window?

My question is definitely different from this question. There the question was about a plugin, and the problem was accessing the chrome:// pages because they are like that, and the browser does not allow accessing them. But I'm writing a regular script (not a plugin) and as such it can always access such pages. The problem in my case is that the print window is not part of the website page but is displayed separately.

I will clarify again. The problem I encountered is with accessing the HTML of a page other than the current page. Using a plugin, it should actually be technically possible, but the browsers block it. So the question of how to do this with a plugin is different from the question of how to do it with a regular script.

post Scriptum. 1. Perhaps it is also possible to make direct use of JS through which Chrome prints, but at the moment it is not relevant for me. The question is how to access the HTML elements as above.

  1. I have seen similar questions (like this one). I didn't see any answers.

答案1

得分: 1

I'm sad to tell you that this is impossible.
In the other hand, you can always use an alert JS library (like sweetalert2) and then just edit the inner HTML of the alert div.

First, include the sweetalert2 script in your HTML <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>.

Then, write a function that creates the popup

function showAlert(){
  Swal.fire(
  'Good job!',
  'You clicked the button!',
  'success'
)

get the popup element and edit its innerHTML

document.getElementsByClassName("swal2-popup")[0].innerHTML = "Write here your HTML for the popup.";

and end the function

}
英文:

I'm sad to tell you that this is impossible.
In the other hand, you can always use an alert JS library (like sweetalert2) and then just edit the inner HTML of the alert div.

First, include the sweetalert2 script in your HTML &lt;script src=&quot;https://cdn.jsdelivr.net/npm/sweetalert2@11&quot;&gt;&lt;/script&gt;.

Then, write a function that creates the popup

function showAlert(){
  Swal.fire(
  &#39;Good job!&#39;,
  &#39;You clicked the button!&#39;,
  &#39;success&#39;
)

get the popup element and edit it's innerHTML

document.getElementsByClassName(&quot;swal2-popup&quot;)[0].innerHTML = &quot;Write here your HTML for the popup.&quot;;

and end the function

`}`

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

function showAlert(){
  Swal.fire(
  &#39;Good job!&#39;,
  &#39;You clicked the button!&#39;,
  &#39;success&#39;
)
document.getElementsByClassName(&quot;swal2-popup&quot;)[0].innerHTML = &quot;Hello, this is an alert with a &lt;button onclick=&#39;alert()&#39;&gt;button&lt;/button&gt;.&quot;;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/sweetalert2@11&quot;&gt;&lt;/script&gt;
&lt;body&gt;
  &lt;p&gt;Hi, this is some inutile text here.
  
  Lorem ipsum dolor sit amet consectetur adipiscing elit praesent, maecenas felis aenean sapien vulputate sed porttitor habitasse auctor, hendrerit eros nibh ligula mollis ornare venenatis. Taciti arcu dapibus aliquam ridiculus nostra posuere velit, facilisi aenean morbi vehicula cursus nullam sapien potenti, tristique pulvinar nulla fames auctor rutrum. Faucibus torquent dui interdum quisque nisi nam maecenas duis, mauris eget class euismod integer curae ultricies, tempor sem felis varius sapien nunc cum.&lt;/p&gt;&lt;button onclick=&quot;showAlert()&quot;&gt;SHOW ALERT&lt;/button&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 01:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定