Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 339

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 392

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 394
Joyber 的博客

场景:jquery.html()获取DOM的代码时如果页面有html代码则发现是被转义的,如果要恢复的话需要做相应处理。

张鑫旭老师的文章:
https://www.zhangxinxu.com/wordpress/2021/01/dom-api-html-encode-decode/

摘要:

///不好用
let str = '<span>by zhangxinxu</span>';
let doc = new DOMParser().parseFromString(str, 'text/html');
console.log(doc.documentElement.textContent);
///没试
let textarea = document.createElement('textarea');
textarea.innerHTML = '<span>by zhangxinxu</span>';
console.log(textarea.childNodes[0].nodeValue);

推荐:
DOM API方法的缺点
DOM API方法利用了浏览器的能力,更容易上手,转义结果也更安全,但是有个不足,那就是只能在浏览器上下文环境中使用。例如,如果是Service Workers环境,或者是Node.js环境中,这个方法就不行了,只能使用传统的字符串处理方法了。

/**
 * 转义HTML标签的方法
 * @param  {String} str 需要转义的HTML字符串
 * @return {String}     转义后的字符串
 */
var funEncodeHTML = function (str) {
    if (typeof str == 'string') {
        return str.replace(/<|&|>/g, function (matches) {
            return ({
                '<': '&lt;',
                '>': '&gt;',
                '&': '&amp;'
            })[matches];
        });
    }

    return '';
};
/**
 * 反转义HTML标签的方法
 * @param  {String} str 需要反转义的字符串
 * @return {String}     反转义后的字符串
 */
var funDecodeHTML = function (str) {
    if (typeof str == 'string') {
        return str.replace(/&lt;|&gt;|&amp;/g, function (matches) {
            return ({
                '&lt;': '<',
                '&gt;': '>',
                '&amp;': '&'
            })[matches];
        });
    }

    return '';
};


  public static function debugRunTime()
        $info = debug_backtrace(0, 1)[0];
        //最后一次记录的耗时
        $last = empty(self::$ts) ? 0 : self::$ts[count(self::$ts) - 1]['time'];
        //累计耗时
        $sum = microtime(1) - RUN_START_TIME;
        $path = str_replace(Yii::app()->basePath, '/', $info['file']);
        self::$ts[] = [
            'time'   => $sum,
            'add'    => $sum - $last,
            'file'   => "({$info['line']}){$path}",
            'memory' => memory_get_usage(),
        ];
}

滚动到指定元素位置


                //滚动到页码位置,把条数选为100条每页
                $jsTo = 'arguments[0].scrollIntoView();';
                $query = Facebook\WebDriver\WebDriverBy::cssSelector('#data2Wrap div.oc-table-pagination div.oc-pagination-wrapper div.ovui-select > div');
                if ( $el = Account::getElementWait($this->driver, $query, 3) ) {
                    $this->msg('设置每页100条');
                    //页码选项100条
                    $this->driver->executeScript($jsTo, [$el]);
                }

获取页面中的一个元素


    /**
     * 获取页面中的一个元素
     * @param RemoteWebDriver $driver
     * @param WebDriverBy $query
     * @param int $timeout
     * @return RemoteWebElement|null
     */
    public static function getElement(RemoteWebDriver $driver, WebDriverBy $query, int $timeout=30)
    {
        while ($timeout>0) {
            try {
                return $driver->findElement($query);
            } catch (\Exception $e) {
                $timeout--;
                sleep(1);
            }
        }
        return null;
    }

    /**
     * 获取页面中的一个元素(等待), elementToBeClickable 参数表示一个可见可点击的元素
     * @param RemoteWebDriver $driver
     * @param WebDriverBy $query
     * @param int $timeout
     * @param string $condition \Facebook\WebDriver\WebDriverExpectedCondition::elementToBeClickable($query)
     * @link https://php-webdriver.github.io/php-webdriver/latest/Facebook/WebDriver/WebDriverExpectedCondition.html
     * @return RemoteWebElement|null
     */
    public static function getElementWait(RemoteWebDriver $driver, WebDriverBy $query, int $timeout=30, $condition='elementToBeClickable')
    {
        try {
            return $driver->wait($timeout, 1000)->until(
               $condition instanceof \Facebook\WebDriver\WebDriverExpectedCondition ? $condition : \Facebook\WebDriver\WebDriverExpectedCondition::$condition($query)
            );
        } catch (\Exception $e) {
            return null;
        }
    }