我们可以将 javascript 插入浏览器中加载的任何网页吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2551067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we insert javascript into any webpage loaded in the browser
提问by pokrate
I am looking into methods to inject javascript into any webpage loaded in the browser, so that I can traverse through the page's DOM. I use JQUERY for my scripting needs. Method should work in all browsers.
我正在研究将 javascript 注入浏览器中加载的任何网页的方法,以便我可以遍历页面的 DOM。我使用 JQUERY 来满足我的脚本需求。方法应该适用于所有浏览器。
I tried using IFRAMEand adding some html into it, but I cant. Please suggest some ways.
我尝试使用IFRAME并在其中添加一些 html,但我不能。请提出一些方法。
回答by dutchflyboy
Try using Greasemonkey: http://www.greasespot.net/. You can use it to execute custom scripts on page load for any website you want. You can find some basic tutorials here: http://wiki.greasespot.net/Tutorials.
尝试使用 Greasemonkey:http: //www.greasespot.net/。您可以使用它在页面加载时为您想要的任何网站执行自定义脚本。您可以在此处找到一些基本教程:http: //wiki.greasespot.net/Tutorials。
回答by sachinjain024
There are a couple of approaches to solve this problem.
有几种方法可以解决这个问题。
Using BookMarklet
使用书签
You can create a simple bookmarklet which injects jQuery on the page and you can open Dev Console in your favorite browser and try out your DOM inspection using jQuery or whatever you want to try out.
您可以创建一个在页面上注入 jQuery 的简单书签,您可以在您喜欢的浏览器中打开开发控制台,并使用 jQuery 或任何您想尝试的方式来尝试您的 DOM 检查。
Use Requestly Chrome Extension
使用 Requestly Chrome 扩展
You can use Requestly Script Ruleto insert scripts on any webpage. Since your post mentions that you need jQuery, Requestly provides an option to include jQuery as well.
您可以使用Requestly Script Rule在任何网页上插入脚本。由于您的帖子提到您需要 jQuery,Requestly 也提供了一个包含 jQuery 的选项。
So with a simple click, you can write jQuery supported code without worrying about how jQuery will come in the page. Check these screenshots for reference :-
因此,只需单击一下,您就可以编写支持 jQuery 的代码,而无需担心 jQuery 将如何进入页面。检查这些屏幕截图以供参考:-
Script Rule Selection
脚本规则选择


Sample Script Supported by jQuery
jQuery 支持的示例脚本


Advantages
好处
A Couple of advantages with using Requestly
使用Requestly 的几个优势
- You can share the script with other Users using the Requestly Share feature
- jQuery is by default supported
- After creating the rule, you can simply disable it once you are not using it.
- Requestly is available on Firefox as well as Chrome so you can run your script cross-browser.
- 您可以使用请求共享功能与其他用户共享脚本
- 默认支持 jQuery
- 创建规则后,您可以在不使用它时简单地禁用它。
- Requestly 在 Firefox 和 Chrome 上都可用,因此您可以跨浏览器运行脚本。
PS: This may be an older post but answering here because the question is still relevant. Disclaimer: I am the founder of Requestly So you blame me if you don't like something.
PS:这可能是一个较旧的帖子,但在这里回答,因为这个问题仍然相关。免责声明:我是 Requestly 的创始人,所以如果你不喜欢某样东西,你可以怪我。
回答by Kronass
I suggest to create a page with two iframes one to navigate to the designated website and other to get DOM Objects. in the first one navigate to the site and then select its HTML and append it in the body of the second Iframe.
我建议创建一个包含两个 iframe 的页面,一个用于导航到指定的网站,另一个用于获取 DOM 对象。在第一个中导航到该站点,然后选择其 HTML 并将其附加到第二个 Iframe 的正文中。
iframe2.contentWindow.document.body.innerHTML = iframe1.contentWindow.document.body.innerHTML
then traverse the DOM Objects inside the second Iframe with your custom functions
然后使用您的自定义函数遍历第二个 Iframe 中的 DOM 对象
回答by Ton van Bart
You could create a bookmarklet (see http://en.wikipedia.org/wiki/Bookmarklet) which in turn can add a node to the page, with the src pointing to where your own javascript is located. Onde the script node gets added it will run. You can find more details on http://www.johnvey.com/features/deliciousdirector/under "how does it work?". This way you can have a bookmark in your bookmarks bar which, when click, will add your script to any page you happen to be on.
您可以创建一个书签(参见http://en.wikipedia.org/wiki/Bookmarklet),它反过来可以向页面添加一个节点,src 指向您自己的 javascript 所在的位置。添加脚本节点后,它将运行。您可以在http://www.johnvey.com/features/deliciousdirector/ 的“它是如何工作的?”下找到更多详细信息。通过这种方式,您可以在书签栏中添加一个书签,单击该书签时,会将您的脚本添加到您碰巧所在的任何页面。
回答by cletus
You can't run Javascript on arbitrary Web pages that you do not control the content of. It would be a hugesecurity hole if that were not true.
您不能在您无法控制其内容的任意网页上运行 Javascript。如果这不是真的,那将是一个巨大的安全漏洞。
Think about it: you could run Javascript and wait for someone to log on to their internet banking and then do something with the characters input.
想一想:您可以运行 Javascript 并等待某人登录他们的网上银行,然后对输入的字符执行某些操作。
回答by Julian Schmuckli
I'm using for Chrome TamperMonkeyto add custom scripts for a specific web page which is as well awesome and I can really recommend it.
我正在使用 Chrome TamperMonkey为特定网页添加自定义脚本,这也很棒,我真的可以推荐它。
回答by Anbu
If I understand correctly, you want to execute a javascript code in any websites you are using in any browser. That means you have to go browser by browser different applications. In chrome supports extension same as firefox, edge, safari browsers supports add-ons. You can add the relevant extension to do that. I am using a scriptingextension, that can run the given script whenever I open the page with same URL given in the script. Likewise you have to find different applications for different browsers.
如果我理解正确,您想在任何浏览器中使用的任何网站中执行 javascript 代码。这意味着您必须通过浏览器浏览不同的应用程序。在 chrome 中支持与 firefox 相同的扩展,edge,safari 浏览器支持附加组件。你可以添加相关的扩展来做到这一点。我正在使用脚本扩展,每当我打开具有脚本中给出的相同 URL 的页面时,它都可以运行给定的脚本。同样,您必须为不同的浏览器找到不同的应用程序。
回答by Thomas Bonini
Take a look at jquery JSONand Wikipedia's JSON page.
Alternatively you can simply add a <script>tag to the document:
或者,您可以简单地<script>向文档添加标签:
$("head").append('<script src="..." type="text/javascript"></script>');
This will load the javascript file.
这将加载 javascript 文件。

