0

I have 30 different web pages, where I am trying to make a script to copy all the texts and paste it to 30 different txt files and save it - all in the background.

So far I successfully created one script for one web page, but I am having problems to create ONE .vbs file that will do it for all 30 pages. I thought I can just copy/paste my code 30x to the bottom of the page and just modify the source/destination of the website and it would work. But it didnt.

With CreateObject("internetexplorer.application")
  .Navigate "http://example.com"
  Do Until .ReadyState = 4
   Wscript.Sleep 100
   Loop

  .Document.execcommand "SelectAll"
  .Document.execCommand "copy"

End With

'paste
   Const ForAppending = 8   

  Dim sFSpec 
  sFSpec = ".\file1.txt" 

  Dim oIE 
  Dim sText 

  Set oIE = CreateObject( "InternetExplorer.Application" ) 
  oIE.Navigate( "about:blank" ) 
  sText   = oIE.document.parentwindow.clipboardData.GetData( "text" ) 
  CreateObject( "Scripting.FileSystemObject" )_ 
  .OpenTextFile( sFSpec, ForAppending, True )_ 
  .WriteLine sText 

   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  'Below, I just copy and paste it, but the code here doesn't work

  With CreateObject("internetexplorer.application")
  .Navigate "http://example1.com" 

   Do Until .ReadyState = 4
   Wscript.Sleep 100
   Loop
  .Document.execcommand "SelectAll"
  .Document.execCommand "copy"

End With

'paste
  Const ForAppending = 8   
  Dim sFSpec1 

  sFSpec1 = ".\dev01-envVar.txt" 

  Dim oIE1 
  Dim sText1 

  Set oIE1 = CreateObject( "InternetExplorer.Application" ) 
  oIE1.Navigate( "about:blank" ) 
  sText1   = oIE1.document.parentwindow.clipboardData.GetData( "text" ) 
  CreateObject( "Scripting.FileSystemObject" )_ 
  .OpenTextFile( sFSpec, ForAppending, True )_ 
  .WriteLine sText 

Or any other easier way than using vbscripting?

Also, IE always gives me this pop-up message - "Do you want to allow this webpage to access your clipboard?" How can I remove that pop up? Remove this popup!

user692942
  • 16,398
  • 7
  • 76
  • 175
EpicDeveloper
  • 23
  • 1
  • 4
  • This statement - *"Or any other easier way than using vbscripting?"* makes the question too broad. See [ask] and [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask). – user692942 Jul 27 '16 at 09:26
  • Why use the an instance of `InternetExplorer.Application` when a [`WinHttpRequest` object](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384106%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) will work just as well without the overheads? – user692942 Jul 27 '16 at 09:29

3 Answers3

0

You marked your question with PowerShell tag so here it is:

"http://example.com", "http://gmail.com" | % {
    $ie = New-Object -ComObject internetexplorer.application
    $ie.Navigate($_)
    while ($ie.ReadyState -ne 4){
        Start-Sleep -Milliseconds 100
    }
    $ie.Document.execCommand("SelectAll") | Out-Null
    Out-File -InputObject $ie.Document.selection.createRange().text `
             -FilePath "D:\Temp\$($ie.Document.title).txt"
    $ie.Quit()
}
  1. I changed this example slightly. It does not need clipboard access and does not pollute this area (you may have something valuable in clipboard), no need to change clipboard access permission, security is improved.
  2. Files are named after page titles, you may change it.
  3. Dont forget to dispose IE component. If it's not released, you end up having many of them as processes in background.
Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27
  • The the code they posted is quite clearly VBScript but just because they erronously tag it [tag:powershell] you'd thought you confuse the issue further. – user692942 Jul 27 '16 at 09:25
  • If your going to use powershell why not suggest the [`System.Net.WebClient`](http://stackoverflow.com/a/509394/692942)? – user692942 Jul 27 '16 at 09:33
  • Simply: because only text is needed. No tags, no style information, no scripts. It's not so easy to parse HTML. – Paweł Dyl Jul 27 '16 at 09:47
  • That's an assumption due to the question not being clear and the approach the OP has taken, fact is none of you should have even attempted answering this question until it was improved. – user692942 Jul 27 '16 at 09:53
  • Thank you @PawełDyl , this really helps. I was thinking using either Powershell or VBScript as I know both would work on what I am trying to do. – EpicDeveloper Jul 27 '16 at 16:22
0

Sinse you mentioned PowerShell tag:

Edited to work in powershell v2.0

Add-Type -AssemblyName system.windows.forms
[System.Windows.Forms.Clipboard]::Clear() # just to be sure

$sitesList = @(
    'http://example.com', 
    <#
        More sites here
    #>
    'http://www.example.com'
)

foreach ($site in $sitesList) {
    $ie = New-Object -ComObject "internetexplorer.application"
    $ie.Navigate($site)

    while ($ie.ReadyState -ne 4) {
        Start-Sleep -Milliseconds 100
    }

    $ie.Document.execCommand( "SelectAll" )
    $ie.Document.execCommand( "copy" )

    $filename = ($site -replace "^http://") + '.txt'


    [System.Windows.Forms.Clipboard]::GetText() | Out-File "D:\$filename" -Force
    [System.Windows.Forms.Clipboard]::Clear()

    $ie.Quit()
}
n01d
  • 1,047
  • 8
  • 22
  • Hi, i copied the exact code but changed the Out-File location to a C:, and it doesnt work.... there is actually an error in line 3... " Missing expression after ','. At line:3 char:29" I removed the comma, and it can only copy to clipboard and it doesnt paste it to the textfile. – EpicDeveloper Jul 27 '16 at 07:03
  • Just remove `,` in the third line :) – n01d Jul 27 '16 at 07:04
  • hi n01d, thanks for your super quick response.... however it can only copy to my clipboard, and not paste/save it to the textfile. – EpicDeveloper Jul 27 '16 at 07:05
  • @EpicDeveloper I don't understand what do you mean... `Get-Clipboard | Out-File` writes your captured clipboard to file. – n01d Jul 27 '16 at 07:07
  • i understand that logic, but did that work for you? because when I ran it, it didn't save the file to my Out-File location. – EpicDeveloper Jul 27 '16 at 07:10
  • @EpicDeveloper Yes, it worked for me. But I just checked: `Get-Clipboard` does not work for powershell 2.0. I use 5.0. Let me check what works in PSv2.0, I'll edit the answer shortly. – n01d Jul 27 '16 at 07:23
  • There is no need to pollute clipboard, check my answer. – Paweł Dyl Jul 27 '16 at 08:18
  • @PawełDyl Somehow i missed your answer O_o EpicDeveloper you should look into Pawel's answer - it is much more correct than mine. – n01d Jul 27 '16 at 08:40
  • The the code they posted is quite clearly VBScript but just because they erronously tag it [tag:powershell] you'd thought you confuse the issue further. – user692942 Jul 27 '16 at 09:24
  • If your going to use powershell why not suggest the [`System.Net.WebClient`](http://stackoverflow.com/a/509394/692942)? – user692942 Jul 27 '16 at 09:33
  • @Lankymart Same reason the topic starter decided not to use it in vbs. – n01d Jul 27 '16 at 14:39
  • Correction, they posted VBS code asking for help with the VBS then added - *"Or any other easier way than using vbscripting?"* which should send alarm bells ringing for any experienced [so] user. Plus that example is powershell I was pointing out using the `InternetExplorer.Application` is pointless we should to advising them to move to xhr. – user692942 Jul 27 '16 at 14:40
  • @Lankymart What are you trying to say? If you have a good and correct answer - just post it. If it is better than mine - it will be accepted as correct. What do you want from all who answers to this question? And why have you removed powershell tag? As far as I can see author accepts PS answers as well. – n01d Jul 27 '16 at 14:47
  • What I'm saying is despite the question being unclear and ambiguous in nature you not only don't leave it well alone you post an answer. Not only that but it's a poor approach at best. We shouldn't be encouraging these types of questions and while people like yourself *attempt* to answer them people will keep posting them. – user692942 Jul 27 '16 at 15:06
0
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
    msgbox window.locationname
    If window.locationname="Scripts" then window.quit
Next

This lists all open shell windows from Explorer and Internet Explorer.

  • Surprised you are encouraging use of `InternetExplorer.Application`, wouldn't have expected you to at least suggest a xhr approach. – user692942 Jul 27 '16 at 09:34
  • That wouldn't answer his question of *in the browser*. His approach is flawed, but that wasn't his question. –  Jul 27 '16 at 09:45
  • The whole question is a mess, it's not clear and the approach is all wrong but still you leave an answer. Way to discourage bad questions @Noodles. – user692942 Jul 27 '16 at 09:50