在 Google Chrome 中将 Javascript 插入网站的简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7171547/
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
What's a simple way in Google Chrome to insert Javascript into sites?
提问by bhuga
I would like to be able to add custom snippets of javascript to any site that matches a regex. This is mostly because of sites that I use daily because of specialized content, but which have a poor design. I want to be able to do something like:
我希望能够将 javascript 的自定义片段添加到任何与正则表达式匹配的站点。这主要是因为我每天使用的网站都有专门的内容,但设计很差。我希望能够执行以下操作:
- Visit site See that sidebar overwrites content
- Whip out developer tools, find div id for sidebar
Edit a snippet of javascript which is executed on document.ready for this domain:
$('#sidebar-right').remove();
- 访问站点 看到侧边栏覆盖了内容
- 推出开发者工具,找到侧边栏的 div id
编辑在 document.ready 上为此域执行的一段 javascript:
$('#sidebar-right').remove();
A bit of searching for user scripts tells me that I need to be writing a Chrome extension, which seems unfortunate and overkill. Is there not an easier way, or an extension which is nothing but a javascript editor that assigns snippets to domains? I'd really like to be able to edit in Chrome itself, or at least have a file that I can just leave open in MacVim all the time. An extension requires unloading/installing to update as far as I can tell.
对用户脚本的一些搜索告诉我,我需要编写一个 Chrome 扩展程序,这似乎很不幸而且有点矫枉过正。有没有更简单的方法,或者只是一个将片段分配给域的 javascript 编辑器的扩展?我真的很希望能够在 Chrome 本身中进行编辑,或者至少有一个我可以一直在 MacVim 中打开的文件。据我所知,扩展程序需要卸载/安装才能更新。
If I just had one big javascript file, that would be fine. I'd just wrap my customizations in domain checks.
如果我只有一个大的 javascript 文件,那就没问题了。我只是将我的自定义包装在域检查中。
Bonus love if I can write in CoffeeScript.
如果我能用 CoffeeScript 写作,那就更棒了。
采纳答案by bhuga
The answer was Tampermonkey.
答案是 Tampermonkey。
https://chrome.google.com/webstore/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo
https://chrome.google.com/webstore/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo
回答by topek
Why not dotjs http://defunkt.io/dotjs/? It's local, you can version it with git, you can easily take it to another computer...
为什么不是 dotjs http://defunkt.io/dotjs/?它是本地的,您可以使用 git 对其进行版本控制,您可以轻松地将其带到另一台计算机...
回答by Ivaylo Toskov
Another alternative that neatly solves the problem is Custom JavaScript for websites. You just need to install the extension, which takes around 2 seconds, and then you can immediately start typing your custom JavaScript for the specified website.
另一种巧妙地解决问题的替代方法是用于网站的自定义 JavaScript。您只需要安装扩展程序,大约需要 2 秒钟,然后您就可以立即开始为指定网站键入自定义 JavaScript。
The extension automatically recognizes the current website, so all you need to do is write your code and click on Save
. You can also easily import jQuery
or your external scripts for convenience.
该扩展程序会自动识别当前网站,因此您只需编写代码并单击Save
。jQuery
为方便起见,您还可以轻松导入或外部脚本。
回答by Chetan
What you're looking for is Greasemonkey. But wait, Greasemonkey is for Firefox only, right? Turns out, you can install Greasemonkey user-scripts as native Chrome add-ons. Just go to userscripts.organd hit the Install button on one of them, and Chrome will automatically convert it into a native add-on. Then, write your own and install it using Chrome.
您正在寻找的是 Greasemonkey。但是等等,Greasemonkey 仅适用于 Firefox,对吗?事实证明,您可以将 Greasemonkey 用户脚本安装为原生 Chrome 插件。只需转到userscripts.org并点击其中一个上的安装按钮,Chrome 就会自动将其转换为原生插件。然后,编写自己的并使用 Chrome 安装它。
Note: This only works in Chrome 4.0.
注意:这仅适用于 Chrome 4.0。
回答by Orestes Kappa
Witchcraftis another Google Chrome extension for loading custom Javascript and CSS, and it is supposedly for more avanced users. The older dotjsproject repository lists Witchcraft as its successor.
Witchcraft是另一个用于加载自定义 Javascript 和 CSS 的 Google Chrome 扩展程序,据说它适用于更高级的用户。较旧的dotjs项目存储库将 Witchcraft 列为其继承者。
回答by xcv58
Custom JavaScript for Websites 2is an alternative to Custom JavaScript for Websites, with some bug fixes and sync scripts across devices feature.
Custom JavaScript for Websites 2是Custom JavaScript for Websites的替代品,具有一些错误修复和跨设备同步脚本功能。
回答by Jordan
Snippets are available directly in Chrome Devtools
片段可直接在 Chrome Devtools 中获得
https://developers.google.com/web/tools/chrome-devtools/javascript/snippets
https://developers.google.com/web/tools/chrome-devtools/javascript/snippets