用 Chrome 内容脚本扩展替换网站中的文本 [英] Replace text in website with Chrome content script extension

查看:74
本文介绍了用 Chrome 内容脚本扩展替换网站中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建 Google Chrome 扩展程序.它的工作是在所有网站上用另一个词替换一个词.

I would like to create Google Chrome extension. Its job is to replace a word with another on all websites.

我有以下 manifest.json 文件:

I have the following manifest.json file:

{
  "name": "My extension",
  "version": "1.0",
  "background_page": "background.html",
  "permissions": [
    "tabs", "http://*/*"
  ],
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["myscript.js"],
      "run_at": "document_end"
    }
  ]
}

myscript.js 中的 javascript 是:

and the javascript in myscript.js is:

< script type="text/javascript" >
    document.body.innerHTML = document.body.innerHTML.replace("uno", "dos");
< /script >

但是这不起作用..我找不到调试内容脚本的方法,只有background.html

However this does not function.. and I cannot find a way to debug the content script, only the background.html

推荐答案

我以 JavaNut13 和 Matt Curtis 的示例为 Reddit 创建了一个别名隐藏器扩展,并为新的清单 2 更新了它.它在 Reddit 上查找用户命名为user1"并将其替换为nobody".根据需要进行修改.

I took the example from JavaNut13 and Matt Curtis to create an alias hider extension for Reddit, and updated it for the new manifest 2. It looks for user on Reddit named "user1" and replaces it with "nobody". Modify as you need.

ma​​nifest.json

{
  "name": "No Alias",
  "version": "0.1",
  "permissions": [
    "https://www.reddit.com/*"
  ],
  "content_scripts": [
    {
      "matches": ["https://www.reddit.com/*"],
      "js": ["myscript.js"],
      "run_at": "document_end"
    }
  ],
  "manifest_version": 2
}

myscript.js

document.body.innerHTML = document.body.innerHTML.replace(new RegExp("user1", "g"), "nobody");

这篇关于用 Chrome 内容脚本扩展替换网站中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆