在 Google Chrome 中将 Javascript 插入网站的简单方法是什么?
我希望能够将自定义的 javascript 片段添加到与正则表达式匹配的任何网站。这主要是因为我每天使用的网站有专门的内容,但设计很差。我希望能够执行以下操作:
- 访问网站 查看侧边栏覆盖内容
- 拿出开发人员工具,查找侧边栏的 div id
编辑在该域的 document.ready 上执行的 javascript 片段:
$('#sidebar-right').remove();
对用户脚本的一些搜索告诉我,我需要编写一个 Chrome 扩展,这似乎很不幸且过大。难道没有一种更简单的方法,或者一个扩展程序,只不过是一个将片段分配给域的 JavaScript 编辑器?我真的很希望能够在 Chrome 本身中进行编辑,或者至少有一个可以在 MacVim 中一直保持打开状态的文件。据我所知,扩展需要卸载/安装才能更新。
如果我只有一个大的 javascript 文件,那就没问题了。我只是将我的自定义包装在域检查中。
如果我能用 CoffeeScript 编写,那就太好了。
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:
- 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();
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.
If I just had one big javascript file, that would be fine. I'd just wrap my customizations in domain checks.
Bonus love if I can write in CoffeeScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
答案是使用 Tampermonkey chrome 扩展。
https://chrome.google.com/webstore/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo
The answer is to use the Tampermonkey chrome extension.
https://chrome.google.com/webstore/detail/dhdgffkkebhmkfjojejmpbldmpobfkfo
为什么不使用 dotjs http://defunkt.io/dotjs/ ?它是本地的,您可以使用 git 对其进行版本控制,您可以轻松地将其带到另一台计算机上......
Why not dotjs http://defunkt.io/dotjs/ ? It's local, you can version it with git, you can easily take it to another computer...
另一种巧妙解决问题的方法是 用于网站的自定义 JavaScript。您只需安装该扩展程序,大约需要 2 秒钟,然后您就可以立即开始为指定网站输入自定义 JavaScript。
该扩展程序会自动识别当前网站,因此您所需要做的就是编写代码并单击
保存
。为了方便起见,您还可以轻松导入jQuery
或外部脚本。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.
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 importjQuery
or your external scripts for convenience.网站自定义 JavaScript 2 是自定义的替代方案用于网站的 JavaScript,具有一些错误修复和跨设备同步脚本功能。
Custom JavaScript for Websites 2 is an alternative to Custom JavaScript for Websites, with some bug fixes and sync scripts across devices feature.
Witchcraft 是另一个用于加载自定义 Javascript 和 CSS 的 Google Chrome 扩展,据说它适合更高级的用户。较旧的 dotjs 项目存储库将 Witchcraft 列为其后继者。
Witchcraft is another Google Chrome extension for loading custom Javascript and CSS, and it is supposedly for more avanced users. The older dotjs project repository lists Witchcraft as its successor.
代码片段可直接在 Chrome 开发者工具中获取
https://developers.google.com /web/tools/chrome-devtools/javascript/snippets
Snippets are available directly in Chrome Devtools
https://developers.google.com/web/tools/chrome-devtools/javascript/snippets
您正在寻找的是油猴。但是等等,Greasemonkey 仅适用于 Firefox,对吗?事实证明,您可以将 Greasemonkey 用户脚本安装为原生 Chrome 插件。只需访问 userscripts.org 并点击其中一个的“安装”按钮,Chrome 就会自动将其转换为本机附加组件。然后,编写您自己的并使用 Chrome 安装它。
注意:这仅适用于 Chrome 4.0。
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.org and 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.
Note: This only works in Chrome 4.0.
2024 年:Violentonkey 是现在专有的的开源替代品>篡改猴
In 2024: Violentonkey is open source alternative to the now-proprietary Tampermonkey