-2

So I want to get HTML elements from multiple pages using JavaScript.

However, as soon as I redirect the user to another page using location.href = "https://www.example.com", the developer console reloads.

Is there a way to prevent this?

EDIT

While sojin's answer shows a way to preserve the text being output, I can't find a way to keep the code, that was input to run after the redirect, running.

gurkensaas
  • 793
  • 1
  • 5
  • 29
  • Maybe it helps: https://stackoverflow.com/questions/11849562/how-to-save-the-output-of-a-console-logobject-to-a-file – Muhammedogz Sep 19 '21 at 14:19
  • 1
    You cannot prevent it with code in your content; it's under control of the browser user. (Really, you don't even know that the user *has* a console or even a browser.) – Pointy Sep 19 '21 at 14:21
  • @Pointy How do you mean that? – gurkensaas Sep 19 '21 at 14:22
  • @muhammedoğuz Not really, since I would have to have a way of running the Javascript from either another tab or from the URL it was just redirected. – gurkensaas Sep 19 '21 at 14:23
  • Do you want this happen to "your own" browser while debugging? – B001ᛦ Sep 19 '21 at 14:27
  • When you load a different page, your own code is obliterated. It's really not clear what you're talking about here. – Pointy Sep 19 '21 at 14:28
  • @Pointy Is there a way for my code not to be obliterated? – gurkensaas Sep 19 '21 at 14:31
  • @B001ᛦ I want it to be easily accessible for all people since I plan on sharing the code that gets the HTML elements on multiple sites. – gurkensaas Sep 19 '21 at 14:36
  • Updating `location.href` loads a new page, and *everything* in the `window` is refreshed. – Pointy Sep 19 '21 at 14:41
  • 1
    You can't do that from inside a page. Do it from outside a page instead: Use a browser extension or a browser automation tool like Selenium or Puppeteer. – CherryDT Sep 19 '21 at 14:46
  • As sad above, updating location.href loads a new page, and everything in the window is refreshed. You can't prevent this behaviour. But your real problem is other. To get elements from new loaded page, you should wait for its **full loading**, every time. Like here: **tell application "System Events" to tell application process "Safari" repeat until ((UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists) or (UI element "Reload this page" of group 2 of toolbar 1 of window 1 exists)) delay 0.1 end repeat end tell** – Robert Kniazidis Sep 20 '21 at 13:47
  • @RobertKniazidis I already did this in AppleScript, you can look at it [here](https://github.com/gurkensaas/StackExchange-Badge-IDs/blob/main/scripts/Applescript.applescript). – gurkensaas Sep 20 '21 at 15:50

1 Answers1

0

enter image description here

You can keep the log by preserving the log in "Chrome"

sojin
  • 2,158
  • 1
  • 10
  • 18
  • While the log is preserved, the code is not running after the site switch. – gurkensaas Sep 19 '21 at 14:25
  • What if they use edge, safari, firefox, opera, etc? – B001ᛦ Sep 19 '21 at 14:26
  • 1
    @gurkensaas Code not running after the switch? didn't understand, could you please add some screenshots or details – sojin Sep 19 '21 at 14:28
  • @B001ᛦ both safari and firefox have a similar option in console settings as similar to chrome, firefox it is presist log, not able to check-in edge, but edge is a copy of chrome so it should have same options – sojin Sep 19 '21 at 14:31
  • I can't really show screenshots here, but once I call the `location.href=...`, my code that is supposed to get the HTML elements stops running. – gurkensaas Sep 19 '21 at 14:32
  • is it after enabling preserve logs?. please turn it off and check it again. it should not be an issue with the log – sojin Sep 19 '21 at 14:33
  • @sojin Still doesn't work. – gurkensaas Sep 19 '21 at 14:41
  • preserve log is to keep the console log in your browser. is that actually what you needed? – sojin Sep 19 '21 at 15:01