Github: github.com/SHENLing062…

  • The brief description + code is available for download on Github
  • Please follow MIT protocol

1. Introduction

Because this year took an examination of the soft examination, this has a little anxiety in his pass line up and down, and then wait for a month, a lot of examination public number began to call into some groups in order to know for the first time whether the results and so on…

As a result, not only did I not get useful information from those groups, but I also felt a lot of anxiety, and it was impossible to keep clicking F5 and frantically refreshing the page while moving bricks.

So do more than a week, the mood is not good sleep, anger and pick up the old line, with JS to write an automatic detection of the results of the website is automatically running script, let Chrome automatically notify me of the results.

Write good for the first time, I quit the group !!!!! Relaxed body and mind!! 😄

If you want to know how to use it, you can go straight to section 2.

If you want to know how this script works, see below, you can tinker with it and use it elsewhere

2. autoRefresh.jsDirections for use

2.1 Setting Chrome to allow Chrome to send notifications

Open chrome’s Settings and add permission to notify the soft test query results

2.2 System Settings Allows Chrome notifications to appear on the desktop

2.3 Enter the target to refresh the website

Soft and examinations website: query.ruankao.org.cn/score

Press F12 to open the console:

Copy the following code into the console, press Enter to run

/* * @Author: ShenLing * @Date: 2021-12-19 14:48:27 * @LastEditors: ShenLing * @LastEditTime: 2021-12-19 15:34:50 */
// Settings: refresh interval, test time string
timeout=prompt('Page refresh interval (seconds) :'); // Automatically updates every 5s
compareText=prompt('Test Time'.'Second half of 2021')

// Initialize variables
count=0; // Refresh times
current=location.href; // Current page link

if(timeout > 0) setTimeout('reload()'.1000 * timeout);
else location.replace(current);

// Automatically reload the page
function reload() {
    // Refresh the page, update the refresh times
    setTimeout('reload()'.1000 * timeout);
    count++;
    console.log(Each `${timeout}Automatic refresh in seconds, refresh times:${count}`);
    
    // Create a frame, ensuring that the contents of the frame are refreshed, rather than wiping the page execution code and stopping the refresh after the entire page is refreshed
    fr4me='<frameset cols=\'*\'>\n<frame id src=\''+current+'\' / > ';
    fr4me+='</frameset>';
    frameDOM = window.frames[0]?window.frames[0].document : null;

    // Call the method to check if the condition is met
    checkInfo(frameDOM ? frameDOM : document)

    // Refresh the nested frame
    with(document) {
        write(fr4me);
        void(close()); }}// Check whether the following conditions are met: the xx field is included. If the conditions are met, enable the browser notification
// This part can be adjusted according to the actual situation
function checkInfo(doc) {
    if (doc && doc.getElementsByTagName('li')) {
        targetDom=doc.getElementsByTagName('li');
        console.log('Current query:${compareText}`);

        for(var i = 0; i < targetDom.length; i++) {
            targetText=targetDom[i].innerText

            if (targetText && targetText === compareText) {
                console.log('Query successful:${targetText}`);
                notify();
                break; }}}}// Browser notification prompt
function notify() {
    // Check whether the browser notification function is enabled. If the function is enabled successfully, the browser can send the notification correctly after the automatic refresh condition is met
    / / can open chrome: / / Settings/content/notifications setting "allow send notification" link
    if(window.Notification && Notification.permission ! = ="denied") {
        Notification.requestPermission(function(status) {
            var n = new Notification(`${compareText}Soft test results', { body: 'Page updated!! Come and see the results. '}); }); }}Copy the code

2.4 Running Results

2.4.1 Enter the interval of automatic page refresh as prompted

2.4.2 Confirm the test time to be automatically detected as prompted

You are advised to enter this parameter for the first runIn the first half of 2021, to test

2.4.3 Test results

Console prompt:

Chrome tips:

Prompt after success, enter the web page, forced to refresh the page, you can stop the automatic program

Don’t forget to run the test again with the same test year

3. Code principle description

3.1 Brief Description

With setTimeout, the webpage is refreshed regularly, and the elements conforming to the target content are detected, and the results will be notified

Note: If setTimeout is directly used, it will cause the code of the control strip to disappear after refreshing once, and the next round of refreshing cannot be carried out. Therefore, the document content of the page must be placed in , and the contents of the frame can be brushed to refresh the content periodically

3.2 Code decomposition description

3.2.1 the main program

// Settings: refresh interval, test time string
timeout=prompt('Page refresh interval (seconds) :'); // Automatically updates every 5s
compareText=prompt('Test Time'.'Second half of 2021')

// Initialize variables
count=0; // Refresh times
current=location.href; // Current page link

// Refresh the page
if(timeout > 0) setTimeout('reload()'.1000 * timeout);
else location.replace(current);
Copy the code

3.2.2 Automatic page reloading method:reload()

Create frameset and frame through JS code, place the original page document into frame, force refresh frame, and call checkInfo() to determine whether the content meets the conditions

Note: You can’t get a document in a frame directly by using document.getelementById (), which is a whole document in a frame, So we need window.frames[0]. Document to get the document in the frame.

// Automatically reload the page
function reload() {
    // Refresh the page, update the refresh times
    setTimeout('reload()'.1000 * timeout);
    count++;
    console.log(Each `${timeout}Automatic refresh in seconds, refresh times:${count}`);
    
    // Create a frame, ensuring that the contents of the frame are refreshed, rather than wiping the page execution code and stopping the refresh after the entire page is refreshed
    fr4me='<frameset cols=\'*\'>\n<frame id src=\''+current+'\' / > ';
    fr4me+='</frameset>';
    frameDOM = window.frames[0]?window.frames[0].document : null;

    // Call the method to check if the condition is met
    checkInfo(frameDOM ? frameDOM : document)

    // Refresh the nested frame
    with(document) {
        write(fr4me);
        void(close()); }}Copy the code

3.2.3 Check whether operating conditions are metcheckInfo()

Since the content of the target year is in

  • , this is to find the corresponding text content in Li.

    For other pages with similar requirements, you can customize the criteria in this section

    // Check whether the following conditions are met: the xx field is included. If the conditions are met, enable the browser notification
    // This part can be adjusted according to the actual situation
    function checkInfo(doc) {
        if (doc && doc.getElementsByTagName('li')) {
            targetDom=doc.getElementsByTagName('li');
            console.log('Current query:${compareText}`);
    
            for(var i = 0; i < targetDom.length; i++) {
                targetText=targetDom[i].innerText
    
                if (targetText && targetText === compareText) {
                    console.log('Query successful:${targetText}`);
                    notify();
                    break; }}}}Copy the code

    3.2.4 Method of browser notificationnotify()

    // Browser notification prompt
    function notify() {
        // Check whether the browser notification function is enabled. If the function is enabled successfully, the browser can send the notification correctly after the automatic refresh condition is met
        / / can open chrome: / / Settings/content/notifications setting "allow send notification" link
        if(window.Notification && Notification.permission ! = ="denied") {
            Notification.requestPermission(function(status) {
                var n = new Notification(`${compareText}Soft test results', { body: 'Page updated!! Come and see the results. '}); }); }}Copy the code