简单写个JS插件替换网页上的文本

背景:

使用Grafana监控面板,随着项目迭代,页面上几百个图表了,相当一部分和错误码有关,逐个图表单替换维护过于繁琐,于是尝试一键替换grafana面板上的错误码(也就替换网页上特定的文本元素),原理上应该很简单,浏览器翻译插件都做到了。

咱们简单实现,思路是执行一个JS脚本,找到要替换的文本,完成替换工作。

*monkey 类的脚本插件(比如 tampermonkey )可以帮忙根据网站地址执行脚本。ScriptCat 是 *monkey类插件中UI比较美观的。下面以此为例,添加Script.
在这里插入图片描述

如下,简单写好了脚本:

// ==UserScript==
// @name         CodeMapper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://你希望脚本起作用的网站/*
// @icon         https://www.google.com/s2/favicons
// @grant        none
// ==/UserScript==

(function() {
    sleep(5000);
    var codeEnum = {
    "1005110001": "codec param error",
    "1005310001": "download url error",
    };

    var labels = document.querySelectorAll("div.css-w166kv-LegendLabel-LegendClickable,div.css-1ns0gep-LegendLabel-LegendClickabel");
    labels.forEach((ele, _, __)=> {
        // console.log(ele.innerHTML)
        // ele.innerHTML = "******"
        for(var code in codeEnum) {
            var msg = codeEnum[code];
            // ele.innerHTML = "*********"
            if(true || ele.innerHTML.indexOf(code)>=0) {
                var target =  String(ele.innerHTML).replace(code, "["+msg+"]"+"("+code+")");
                console.log(target);
                ele.innerHTML = target;
            }
        }

    })
})();

发现 *monkey类工具,不能保证我网页异步加载完成了再执行脚本,也不支持手动触发脚本的执行。

不过也不难解决,如下图,可以通过,定时弹窗 confirm来控制脚本执行。

// ==UserScript==
// @name         CodeMapper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match       http://你希望脚本起作用的网站/*
// @icon         https://www.google.com/s2/favicons
// @grant        none
// ==/UserScript==

(function () {
    var inter_ = setInterval(() => {
        var res__ = confirm("run stript?")
        if (!res__) {
            // 时机未到
            return;
        }
         clearInterval(inter_);

        var codeEnum = {
            "1005110001": "codec param error",
            "1005310001": "download url error",
        };

        var labels = document.querySelectorAll("div.css-w166kv-LegendLabel-LegendClickable,div.css-1ns0gep-LegendLabel-LegendClickabel");
        labels.forEach((ele, _, __) => {
            // console.log(ele.innerHTML)
            // ele.innerHTML = "******"
            for (var code in codeEnum) {
                var msg = codeEnum[code];
                // ele.innerHTML = "*********"
                if (true || ele.innerHTML.indexOf(code) >= 0) {
                    var target = String(ele.innerHTML).replace(code, "[" + msg + "]" + "(" + code + ")");
                    console.log(target);
                    ele.innerHTML = target;
                }
            }

        })
    }, 5000)

})();

还是不太好用,让网页上显示一个小按钮,点击后执行,才是比较好的交互方式。
前端不是很熟悉,让GPT来帮忙吧。

How to inject a button into a web page by JavaScript, require the button to hover in the top right corner of the web page, require the button to be a white button with rounded corners with a shadow, the button to have an image icon, when clip that button, show hello world confirm dialog

o inject a button into a web page using JavaScript with the specified requirements of having a button hover in the top right corner, being a white button with rounded corners and a shadow, and having an image icon, along with the functionality to show a confirm dialog when clicked, you can follow these steps:

Create the button element using the createElement method and set its properties such as id and class.

Style the button using CSS to achieve the desired appearance by setting its background color, border radius, box shadow, position, and other relevant styles.

Add the image icon to the button element either by setting its innerHTML or creating an img element as a child of the button.

Attach an event listener to the button to listen for the click event. Inside the event listener, show the confirm dialog using the confirm function.

Here’s an example code snippet that demonstrates injecting a button with the specified requirements:

// Step 1: Create the button element
var button = document.createElement('button');
button.id = 'my-button';
button.className = 'hover-button';

// Step 2: Style the button
button.style.backgroundColor = 'white';
button.style.borderRadius = '10px';
button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.3)';
button.style.position = 'fixed';
button.style.top = '20px';
button.style.right = '20px';

// Step 3: Add the image icon to the button
var iconImg = document.createElement('img');
iconImg.src = 'path/to/icon.png'; // Replace with the path to your image icon
iconImg.alt = 'Icon';
button.appendChild(iconImg);

// Step 4: Attach an event listener to the button
button.addEventListener('click', function() {
  var confirmed = confirm('Hello World!');
  if (confirmed) {
    // Add your desired functionality here
    console.log('Confirmed');
  }
});

// Step 5: Attach the button to the document or container
document.body.appendChild(button);
In this example, the button is given the class hover-button in addition to the specified styles. You can define the hover-button class in your CSS file or inline using the style attribute to further customize the button’s appearance.

Make sure to replace ‘path/to/icon.png’ with the actual path to your desired image icon.

When the button is clicked, a confirm dialog with the message “Hello World!” will be shown. If the user confirms the dialog, the code inside the if statement will execute (in this case, logging “Confirmed” to the console). You can modify the code inside the if statement to perform any desired functionality.

Adjust the positioning, styling properties, and confirm dialog message as needed to fit your requirements.

好了结合他的回答,改一下上面的代码:

// ==UserScript==
// @name         CodeMapper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://你希望脚本起作用的网站/*
// @icon         https://www.google.com/s2/favicons
// @grant        none
// ==/UserScript==

function button_do(icon, f) {
    // Step 1: Create the button element
    var button = document.createElement('button');
    button.id = 'my-button';
    button.className = 'hover-button';

    // Step 2: Style the button
    button.style.backgroundColor = 'white';
    button.style.borderRadius = '10px';
    button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.3)';
    button.style.position = 'fixed';
    button.style.top = '20px';
    button.style.right = '20px';

    // Step 3: Add the image icon to the button
    var iconImg = document.createElement('img');
    iconImg.src = icon; // Replace with the path to your image icon
    iconImg.alt = 'Icon';
    button.appendChild(iconImg);

    // Step 4: Attach an event listener to the button
    button.addEventListener('click', function() {
        f();
    });

    // Step 5: Attach the button to the document or container
    document.body.appendChild(button);
}

(function () {
    button_do("https://www.google.com/s2/favicons", ()=>{
        var codeEnum = {
            "1005110001": "codec param error",
            "1005310001": "download url error",
        };

        var labels = document.querySelectorAll("div.css-w166kv-LegendLabel-LegendClickable,div.css-1ns0gep-LegendLabel-LegendClickabel");
        labels.forEach((ele, _, __) => {
            // console.log(ele.innerHTML)
            // ele.innerHTML = "******"
            for (var code in codeEnum) {
                var msg = codeEnum[code];
                // ele.innerHTML = "*********"
                if (true || ele.innerHTML.indexOf(code) >= 0) {
                    var target = String(ele.innerHTML).replace(code, "[" + msg + "]" + "(" + code + ")");
                    console.log(target);
                    ele.innerHTML = target;
                }
            }

        })
    });
})();

效果如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要替换Chrome插件,可以按照以下步骤进行: 首先,找到你想要替换的Chrome插件。你可以在Chrome商店或其他第三方网站上寻找替代插件。确保新插件与你的需求相匹配,并且在细节和功能方面与原插件相似。 然后,在你的Chrome浏览器中禁用或卸载原插件。你可以在浏览器设置的“扩展程序”部分找到已安装的插件列表。找到原插件并选择“禁用”或“删除”选项。 接下来,下载并安装新的插件。你可以通过将其添加到Chrome浏览器的“扩展程序”部分来安装新插件。点击“加载已解压的拓展程序”按钮,然后选择新插件的文件夹。 安装完成后,确保新插件已启用。在“扩展程序”部分中找到新插件并开启它。有些插件可能需要额外的配置或许可证才能正常运行,请按照插件提供的指示进行操作。 最后,从现在开始,你的Chrome浏览器将使用新的插件。请确保它正确运行并满足你的需求。如果你发现新插件不适合你,你可以重复上述过程,重新安装原插件或尝试其他替代方案。 综上所述,替换Chrome插件可以通过禁用或卸载原插件,并安装新的插件来实现。这样做可以为你提供更好的功能和体验。 ### 回答2: 要替换Chrome插件JS,可以按照以下步骤进行: 1. 确定要替换的Chrome插件以及其对应的JS文件。可以在Chrome浏览器的扩展程序页面找到插件,并打开详情页。 2. 打开Chrome浏览器扩展程序的开发者模式。在Chrome地址栏中输入"chrome://extensions/",然后切换到开发者模式。 3. 找到要替换插件,点击它的"背景页"链接,在打开的新页面中找到对应的JS文件。 4. 复制和保存旧的JS文件内容,将其备份到其他位置,以便出现问题时可以还原。 5. 编写新的JS代码,实现所需的功能。 6. 打开Chrome开发者工具,或者使用其他文本编辑器,将新的JS代码粘贴到插件JS文件中。 7. 保存JS文件,然后返回到扩展程序页面。 8. 点击页面上的"重新加载"按钮,以应用新的JS代码。 9. 使用Chrome浏览器测试插件的功能,确保新的JS代码能够正常工作。 10. 如果遇到任何问题或错误,可以通过将备份的旧JS代码粘贴回插件JS文件中,重新加载插件恢复到替换之前的状态。 总结:替换Chrome插件JS需要备份旧代码,编写新的代码,然后将新代码粘贴到插件JS文件中,最后重新加载插件应用新代码。在整个过程中,需要确保新代码的正确性和功能的正常运行。 ### 回答3: JSJavaScript替换Chrome插件的方法有多种。下面我将介绍其中的一种方法: 首先,打开Chrome浏览器,点击右上角的菜单按钮,选择“更多工具”,然后选择“扩展程序”。 在扩展程序页面,找到需要替换插件,并单击右侧的“删除”按钮,以卸载该插件。 然后,在Chrome浏览器中搜索你想要替换插件,找到适合自己需求的替代品,并在插件商店页面点击“添加至Chrome”按钮。 在弹出的确认页面中,点击“添加扩展程序”按钮,进行安装。 安装完成后,该插件会自动出现在扩展程序页面中,并且会自动启用。 请注意,在替换插件时,一定要验证插件的来源和评价,确保选择的插件是可靠的。 此外,还可以通过手动下载插件安装文件的方式,打开扩展程序页面,启用开发者模式,选择“加载已解压的扩展程序”,然后选择下载的插件文件夹,点击“选择文件夹”按钮进行安装。 总之,替换Chrome插件可以通过删除旧插件并安装新插件的方式实现,保证新插件的来源可靠,并满足自己的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值