动态 版块 发帖 消息 我的
小绿叶技术博客
小绿叶技术博客
小绿叶技术Lv154   
ubuntu shell 和 windows bat 脚本 调用 google 浏览器自动打开网站 自动刷课脚本     

操作说明:
1. 需要使用脚本配置选择的浏览器打开网站进行登录,这样就保存伦 cookie, 脚本打开浏览器访问网站就不需要 重新登录,直接访问内容  



# ubuntu shell 脚本 


#!/bin/bash
# cookie : google 浏览器 > 隐私和安全 > 第三方cookie > 允许第三方cookie 
# 网站设置 > 自动撤销网站使用权限 
# 设置要打开的网页URL
urllist=(
eisc.cn   work.eisc.cn 0
)

MycookieDir=./test/Mycookie
sudo mkdir -p $MycookieDir
sudo chmod 777 -R $MycookieDir

echo "" > ./chrome_pid_16666.pid

# 设置Google Chrome浏览器的路径(请根据实际情况修改)
chrome_path="/usr/bin/google-chrome"
#chrome_path="/opt/google/chrome/chrome"

# 打开网页并进入无限循环
while true; do
    # 打开网页
    ((count++))
    PID=16666
    for urli in ${urllist[*]}
    do 
        # nohup $chrome_path --new-window "$urli" --profile-directory="Default" --user-data-dir=$MycookieDir/chrome_$PID > /dev/null 2>&1 & echo $! >> chrome_pid_$PID.pid
        # 这个是不共享 cookie ,而指定新的 cookie 的目录

        nohup $chrome_path --new-window "$urli" --profile-directory="Default" --remote-debugging-port=9222 > /dev/null 2>&1 & echo $! >> chrome_pid_$PID.pid
        

        # 等待1分钟(60秒)
        t=2
        for((i=1;i>0;i--))
        do
            echo "等待 $t 秒后打开新网页网页 $urli"
            sleep $t
        done
        
        if [[ $urli = 0 ]]
        then 

            echo "网页已经全部打开等待 $settime 秒钟后关闭重新打开"

            for((ti=900;ti>1;ti--))
            do 
               echo "当前时间剩余: $ti  秒钟"
               sleep 1
            done 

            IDlist=(`wmctrl -l | grep Chrome | awk -F" " '{print $1}'`); echo ${IDlist[*]}
            for((id=1;id<${#IDlist[*]};id++))
            do 
                # 从1 开始,是不关闭开始打开的登录页面
                echo "xdotool windowclose ${IDlist[$id]} "
                xdotool windowclose ${IDlist[$id]} ; sleep 0.1
            done
        fi
    done

    if [[ $count > 0 ]]
    then 
        echo "break 将其注释掉,会一直跑"
        #break               # 将其注释掉,会一直跑
    fi 

done


# sudo apt-get install xdotool wmctrl -y 
# wmctrl -l                           # 查询打开的窗口
# xdotool windowclose 0x04b84012      # 关闭窗口  0x04b84012  
# window_id=$(xdotool getactivewindow)# 获取当前动态窗口ID 



#    https://lms.ouchn.cn/course/60000065150/learning-activity/full-screen#/60003505217 https://lms.ouchn.cn/course/60000065150/learning-activity/full-screen#/60003505224 https://lms.ouchn.cn/course/60000065150/learning-activity/full-screen#/60003505247 https://lms.ouchn.cn/course/60000065150/learning-activity/full-screen#/60003505305 https://lms.ouchn.cn/course/60000065150/learning-activity/full-screen#/60003505334 https://lms.ouchn.cn/course/60000065150/learning-activity/full-screen#/60003505378 https://lms.ouchn.cn/course/60000065445/learning-activity/full-screen#/60004041602 https://lms.ouchn.cn/course/60000065445/learning-activity/full-screen#/60004041550 https://lms.ouchn.cn/course/60000065445/learning-activity/full-screen#/60004041553 https://lms.ouchn.cn/course/60000065445/learning-activity/full-screen#/60004041561 https://lms.ouchn.cn/course/60000065445/learning-activity/full-screen#/60004041575 0







#--------- windows bat 脚本 -----------#

@echo off
setlocal EnableDelayedExpansion

:: 设置要打开的网页URL,注意空格和0的表示
set "urllist=  eisc.cn   work.eisc.cn 0"
:: 设置Mycookie目录
set "MycookieDir=.\test\Mycookie"
if not exist "!MycookieDir!" mkdir "!MycookieDir!"

:: 创建一个文本文件来存储Chrome进程ID
echo. > chrome_pid_16666.pid

:: 设置Google Chrome浏览器的路径(请根据实际情况修改)
::set "chrome_path=C:\install\browser\Chrome\Application\chrome.exe"
set "chrome_path=C:\Program Files\Google\Chrome\Application\chrome.exe"
::set "chrome_path=C:\Users\liufang\AppData\Local\Google\Chrome\Application\chrome.exe"



:: 无限循环
:loop
    :: 清空屏幕
    cls

    :: 打开网页
    set /a count+=1             
    :: 计数,下面函数判断这个变量在规定多少次就被退出

    set "PID=16666"
    for %%i in (!urllist!) do (
        echo 打开域名: "%%i"
        start "" "!chrome_path!" --new-window "%%i"  >nul 2>&1
        echo Process ID: !PID! >> chrome_pid_!PID!.pid

        :: 等待1分钟(2秒)
        set /a t=2
        for /L %%j in (1,1,2) do (
            echo 等待 !t! 秒后打开新网页 %%i
            ping -n 2 127.0.0.1 >nul
            set /a t+=1
        )

        :: 检查是否为结束标记
        if "%%i"=="0" (
            echo 网页已经全部打开,等待900秒钟后关闭重新打开
            set /a ti=300
            :wait
                if !ti! leq 0 goto closechrome
                echo 当前时间剩余: !ti! 秒钟
                set /a ti-=1
                ping -n 2 127.0.0.1 >nul
                goto wait
        )
    )

    :: 关闭Chrome窗口
    :closechrome
        echo Closing all Chrome windows...
        tasklist /fi "imagename eq chrome.exe" | find /i "chrome.exe" > chrome_list.txt
        :: tasklist 列出任务进程
        for /f "tokens=1" %%a in (chrome_list.txt) do (
            echo Closing window with PID %%a
            taskkill /f /pid %%a
            :: 关闭任务进程
        )
        del chrome_list.txt

    :: 检查是否需要退出循环
    if !count! gtr 0 (
        echo break 将其注释掉,会一直跑
        rem break               # 将其注释掉,会一直跑
    )

    :: 等待5秒后继续
    timeout /t 5 >nul
    goto loop

endlocal




 2  已被阅读了322次  楼主 2024-05-29 17:31:21
回复列表

回复:ubuntu shell 和 windows bat 脚本 调用 google 浏览器自动打开网站 自动刷课脚本

guest
登录之后更精彩~
Powered by HadSky 7.12.10
© 2015-2024 PuYueTian
您的IP:44.221.66.130,2024-06-20 14:55:39,Processed in 0.02724 second(s).
Powered by HadSky
小绿叶技术博客