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

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

问题描述

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



我有以下manifest.json文件:



< pre $ {
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是:

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

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

解决方案

我从JavaNut13和Matt Curtis的例子中为Reddit创建了一个别名hider扩展,并将其更新为新的清单2.它在Reddit上查找名为user1的用户,并用nobody替换它。您可以根据需要进行修改。
$ b manifest.json

  {
name:No Alias,
version:0.1,
permissions:[
https:// www。 reddit.com/*

content_scripts:[
{
匹配:[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); 


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

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"
    }
  ]
}

and the javascript in myscript.js is:

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

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

解决方案

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.

manifest.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天全站免登陆