问题描述
我想在我的 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 中实时添加脚本代码在浏览器打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!