2017-02-13 18 views
0

我想创建一个applescript。Applescript - 重新加载网页直到HTML包含特定文本

我需要在Chrome(或Safari)中自动重新加载当前网页,直到在HTML中出现一些特定的关键字。找到这个关键字后,停止重新加载。

该网页有一个带广告的区域。只要我能看到一些特定的广告,我就需要重新加载页面。这意味着关键字adPraha在HTML中。

请问您能帮我吗?

set keyword to "adPraha" 
tell application "Safari" 
    -- get the current window 
    set myWindow to (get current tab of window 1) 

    repeat 
     do JavaScript "window.location.reload()" in myWindow 


     -- wait for the page to load   
     repeat until ((do JavaScript "document.readyState;" in myWindow) is equal to "complete") 
      delay 0.5 
     end repeat 

     set pageContent to text of myWindow 

     if pageContent contains keyword then 

     else 

     end if 

     delay 2 -- wait a bit before running again 

    end repeat 
end tell 
+0

这个脚本究竟有什么问题?它似乎对我有用。我在'else/end if'之间添加了'if/else'和另一个之间的显示对话框,并且当'pageContent'包含'keyword'时成功检测到脚本。你在寻找'exit repeat'命令吗?将它放在'else/endif'之间,或者将“contains keyword”替换为“不包含关键字”,它会在丢失关键字时退出重复。 –

回答

0

该脚本会通过对话框告诉您该页面检测到关键字。它只适用于Safari。 Google Chrome以不同的方式执行JavaScript:

tell application "Safari" 

    set keyword to "adPraha" 
    repeat 
     set myWindow to current tab of first window 
     activate 

     do JavaScript "window.location.reload()" in myWindow 

     repeat while (do JavaScript "document.readyState" in document 1) is not "complete" 
      delay 0.5 
     end repeat 

     set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow 

     if pageContent contains keyword then 
      display dialog "found it" 
      exit repeat 
     end if 
     delay 2 -- wait a bit before running again 
    end repeat 
end tell 

这是否适合您?

+0

非常感谢!这项工作与网络中的文字。但我想在HTML源代码中搜索关键字。 Web中的一些标签或HTML的一些值。是可以搜索它吗?我已经找到了解析HTML的一些脚本,但它不能这样做。 –

+0

该脚本已经在搜索HTML源代码。 :) –