javascript – Chrome扩展 – 用于在任何页面上运行js的简单内容脚本

前端之家收集整理的这篇文章主要介绍了javascript – Chrome扩展 – 用于在任何页面上运行js的简单内容脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Chrome扩展程序 – 内容脚本 – 我如何编写一个简单的内容脚本,将在每个页面上执行一个类似 javascript的警报(“hello”)加载..我的意思是当我去一些页面,如google.com的消息应该出现..或者如果我重新加载任何页面,消息应该显示为Im newvbiew.请帮忙

我到目前为止有这个json文件

  1. {
  2. "name": "Highlight some phrases","description": "Hightlight some pre defined text from websql database after page loads","version": "0.1","permissions": [
  3. "tabs","<all_urls>"
  4. ],"browser_action": {
  5. "default_icon": "icon.png","default_popup": "popup.html"
  6. },"content_scripts": [
  7. {
  8. "matches": [
  9. "http://*/*","https://*/*"
  10. ],"js": ["content.js"]
  11. }
  12. ],"background": {
  13. "page": "background.html"
  14. },"manifest_version": 2
  15. }

解决方法

如果您需要的是在每个加载的页面上提醒你,下面是一个简单的演示:

manifest.json的:

  1. {
  2. "name": "Highlight some phrases","browser_action": {
  3. "default_icon": "icon.png"
  4. },"content_scripts": [
  5. {
  6. "matches": [
  7. "http://*/*","https://*/*"
  8. ],"js": ["content.js"],"run_at": "document_end" // pay attention to this line
  9. }
  10. ],"manifest_version":2
  11. }

这里是content.js中的内容

  1. // alert("hello");
  2. document.body.style.background = 'yellow';

是的,够了当然不要忘了在这两个文件的同一个目录下添加一个名为icon.png的图标.然后在Chrome中测试.

猜你在找的JavaScript相关文章