const_wjm 2024-05-30 11:42 采纳率: 40%
浏览 8
已结题

注入提示语在某页面上,前端判断是否首次登陆并显示对应提示语

private injection() { 
     let hintHook = `
        const hasVisited = localStorage.getItem('hasVisited') !== null;
        const hintWidget = document.createElement('div');
        const hintText = document.createElement('p');
        hintText.id = 'hintTextDiv';
        if (!hasVisited) {
            hintText.textContent = '首次';
        } else {
            hintText.textContent = '二次';
        }
      hintWidget.appendChild(hintText);
        document.body.appendChild(hintWidget);
        localStorage.setItem('hasVisited', 'true');`;

this channelWindow = new BrowserWindow({ width: 800, height: 600 })
        this.channelWindow.webContents.on('did-finish-load', () => {
            this.channelWindow?.webContents.executeJavaScript(hintHook,  true);}
}



这是注入盒子在某页面里来当做提示语,用来提示是否首次登录。打开登录页后提示语会显示'首次',但是加载跳转后会变成'二次',应该加载页面重新获取hasVisited再判断导致的吧,'二次'的提示语要在非首次登录提示的,要怎么解决呢

  • 写回答

2条回答 默认 最新

  • 阿里嘎多学长 2024-05-30 11:42
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    问题描述中指出,在Electron应用中,通过JavaScript注入的方式在页面上显示提示语,但存在一个问题:在页面首次加载时提示语正确显示为“首次”,但在页面跳转后,提示语错误地变成了“二次”。这可能是因为在页面跳转时,localStorage中的hasVisited状态没有被正确地重新检查。

    要解决这个问题,你需要确保在每次页面加载完成后都重新检查localStorage中的hasVisited状态,并根据这个状态来更新提示语。以下是修改后的代码示例:

    private injection() {
        let hintHook = `
            const updateHintText = () => {
                const hasVisited = localStorage.getItem('hasVisited') !== null;
                const hintTextDiv = document.getElementById('hintTextDiv');
                if (hintTextDiv) {
                    if (!hasVisited) {
                        hintTextDiv.textContent = '首次';
                    } else {
                        hintTextDiv.textContent = '二次';
                    }
                }
            };
    
            const hintWidget = document.createElement('div');
            const hintText = document.createElement('p');
            hintText.id = 'hintTextDiv';
            hintText.textContent = '首次'; // 默认显示首次
            hintWidget.appendChild(hintText);
            document.body.appendChild(hintWidget);
    
            // 更新提示语
            updateHintText();
    
            // 监听页面跳转事件,重新更新提示语
            window.addEventListener('load', updateHintText);
        `;
    
        this.channelWindow.webContents.on('did-finish-load', () => {
            this.channelWindow.webContents.executeJavaScript(hintHook, true);
        });
    }
    

    在这个修改后的代码中,我们首先定义了一个updateHintText函数,该函数会检查localStorage中的hasVisited状态,并更新页面上的提示语。然后,我们在页面加载时创建提示语元素,并将其默认文本设置为“首次”。接着,我们使用addEventListener监听windowload事件,这样无论何时页面加载完成,都会调用updateHintText函数来更新提示语。

    参考资料:

    1. Electron - webContents.executeJavaScript
    2. MDN - localStorage
    3. MDN - EventTarget.addEventListener

    请注意,上述代码示例假设你使用的是TypeScript,并且this.channelWindow是一个有效的Electron窗口引用。如果你的实现细节有所不同,请根据实际情况调整代码。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月7日
  • 修改了问题 5月30日
  • 创建了问题 5月30日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,