如何在浏览器中实时在HTML中添加脚本代码将其打开 [英] How to add a script code in my html in real time on browser open it

查看:862
本文介绍了如何在浏览器中实时在HTML中添加脚本代码将其打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的html页面中添加脚本代码功能,尝试使用$(element).append('my script'),但是此方法添加的内容像文本,而不是类似的代码。

I want add a script code function in my html page, i'm tried using $(element).append('my script') but, this method has add like a text, no like code.

我该怎么做?

我在页面控制台中执行了以下代码:

I executed this code in my page console:

document.querySelector("body").append('<script> function validarValorElemento(e){   var xpathWay = "xpath=//"+e.target.tagName+"[@class=\'" + e.target.className+"\']"    var elementValue = e.target.textContent  alert(xpathWay) alert(elementValue)}</script>');

这是结果。

推荐答案

IF 我正确理解了您的问题-也就是说,如果您想将代码注入到无法访问源代码的现有网页中,那么:

IF I understand your question correctly -- that is, if you want to inject code into an existing webpage where you don't have access to the source code -- then:

最简单的方法是使用名为 TamperMonkey -它允许您根据URL将javascript注入任何网页。

The simplest way to do this is with a browser extension called TamperMonkey - it allows you to inject javascript into any webpage, based on the URL.

TamperMonkey脚本安装(或编写)在每个人的浏览器中,它们只会影响该台计算机上的网页。因此,如果您的朋友也希望看到相同的效果,他还需要安装TamperMonkey扩展并添加您的脚本-这就是为什么现在全球有数百万TamperMonkey用户的原因。非常棒的产品-非常感谢 Jan Biniok

TamperMonkey scripts are installed (or written) in each person's browser, individually, and they only effect the webpage on that one computer. So, if your friend also wants to see the same effect, he also needs to install the TamperMonkey extension and add your script - which is why there are now millions of TamperMonkey users around the world. Hugely amazing product - many thanks to Jan Biniok.

GreasyFork 脚本存储库。

TamperMonkey来自先前的扩展名GreaseMonkey,因此在您看到该名称的变体中,它可能也与TamperMonkey有关。 TamperMonkey的文档在这里:

TamperMonkey comes out of a previous extension called GreaseMonkey, so anywhere you see variations of that name, it probably also relates to TamperMonkey. The documentation for TamperMonkey is here:

https:// tampermonkey.net/documentation.php

这篇关于如何在浏览器中实时在HTML中添加脚本代码将其打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆