Javascript document.write-in-Chrome扩展

Javascript document.write-in-Chrome扩展,javascript,dom,google-chrome-extension,window.location,document.write,Javascript,Dom,Google Chrome Extension,Window.location,Document.write,我是新手,所以请容忍我。我正在尝试编写一个chrome扩展,它可以实现以下功能: 检测www.website.com/anypage.html。如果检测到此网站,请执行以下操作 不要加载URL 相反,写一个带有超链接的空白文档到www.website.com/anypage.html?ie=UTF8 脚本设置为在文档开始时运行(在清单中) 这是我的密码: 检测URL: var regExp = /website.com/gi; var match = 0; testString = window

我是新手,所以请容忍我。我正在尝试编写一个chrome扩展,它可以实现以下功能:

  • 检测www.website.com/anypage.html。如果检测到此网站,请执行以下操作
  • 不要加载URL
  • 相反,写一个带有超链接的空白文档到www.website.com/anypage.html?ie=UTF8
  • 脚本设置为在文档开始时运行(在清单中)

    这是我的密码:

    检测URL:

    var regExp = /website.com/gi;
    var match = 0;
    testString = window.location.href.toString();
    if(regExp.test(testString) { 
    match = 1;
    
    使用UTF8编码标记编写带有URL链接的空白文档:

    document.write("<a href=" + window.location.href + "?ie=UTF8>Title of Link</a>");
    
    document.write(“”);
    
    这不符合预期,只显示一个空白页。有人有什么想法吗

    谢谢


    编辑:以下是完整代码:

    checklink(); // If there is a match, then checklink will return a 1. If it's already     tagged, it will return a 5.
    var matchLink = null;
    if (checklink() === 1) {
    matchLink = window.location.href.toString();
    
    if (checklink() != 1) {
    matchLink = null;
    
    function checklink() { //checks to see if the current URL matches website.com
    var regExp = /website.com/gi,
    testString = window.location.href.toString(),
    match = 0,
    tagged = 0;
    
    if (regExp.test(testString)) { //if there is a match, returns 1
    match = 1;
    
    
    var regExp2 = /UTF8/gi;
    if (regExp2.test(testString)) { //if UTF8 is found, then it returns 5
    tagged = 5;
    
    return(match + tagged);
    
    
    function tagUTF() {
    if (matchLink) {
    var newLink = matchLink + "?ie=UTF8";
    document.write("<a href=\"" + newLink + "\">Link</a>");
    
    
    if (matchLink) {
    tagUTF();
    }
    
    checklink();//如果存在匹配项,则checklink将返回1。如果它已经被标记,它将返回一个5。
    var matchLink=null;
    如果(checklink()==1){
    matchLink=window.location.href.toString();
    如果(checklink()!=1){
    matchLink=null;
    函数checklink(){//检查当前URL是否与website.com匹配
    var regExp=/website.com/gi,
    testString=window.location.href.toString(),
    匹配=0,
    标记=0;
    if(regExp.test(testString)){//如果存在匹配项,则返回1
    匹配=1;
    var regExp2=/UTF8/gi;
    如果(regExp2.test(testString)){//如果找到UTF8,则返回5
    标记=5;
    返回(匹配+标记);
    函数tagUTF(){
    如果(匹配链接){
    var newLink=matchLink+“?ie=UTF8”;
    文件。填写(“”);
    如果(匹配链接){
    tagUTF();
    }
    
    chrome内容脚本可以访问DOM,因此您可以使用DOM操纵方法或innerHTML将当前页面主体元素的内容替换为具有锚定标记的新节点:

    document.body.innerHTML = "<a href=" + window.location.href + "?ie=UTF8>Title of Link</a>";
    
    document.body.innerHTML=“”;
    
    请注意,这假设执行DOM操作的JavaScript已作为“内容脚本”正确添加到您的Chrome扩展中:

    编辑: 以下是我在本地使用的代码:

    manifest.json

    {
      "name": "Test",
      "version": "1.0",
      "description": "Test",
      "permissions": [
        "<all_urls>"
      ],
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["content-script.js"],
          "run_at": "document_end"
        }
      ]
    }
    
    {
    “名称”:“测试”,
    “版本”:“1.0”,
    “说明”:“测试”,
    “权限”:[
    "";
    
    根据@ldiqual,我们需要看到更多的代码,但我首先想到的是你没有在锚中引用URL…document.write(“”);在编辑中添加了完整的代码。还为锚中的URL添加了引号。但是,我认为这不是问题,因为一个简单的
    document.write(Test)
    也不起作用。我试过了-不起作用,它给了我一个TypeError异常。正在进行DOM操作的JS文件作为内容脚本添加到扩展中,并设置为在document_开始时运行。我还尝试了document_结束和document_空闲,但仍然没有骰子。进一步检查后,似乎例外情况是document.body为null,这解释了它为什么不接受innerHTML。但是document.body为什么为null?我用我在本地使用的代码编辑了答案。我在internet上做了一些测试,它在我尝试的所有页面上都起作用(包括stackoverflow!)。我不知道为什么doucment.body对您来说是空的,特别是当您在doucment\u端使用run\u时
    document.body.innerHTML = "<a href='test'>test</a>";