简体   繁体   English

我正在尝试做一个chrome查找和替换扩展程序,但是它不起作用

[英]I'm trying to make a chrome find-and-replace extension, but it won't work

I've looked through four or five examples on here and other sites, and they all give me a similar way to do it, but it isn't working. 我在这里和其他站点浏览了四个或五个示例,它们都为我提供了一种类似的方法, 但是它没有用。

//Manifest //表现

{
    "manifest_version": 2,
    "name": "Screw My Ex",
    "permissions": [
    "https://www.facebook.com/*"
    ],
    "description": "Replaces her name",
    "version": "0.1",
    "content_scripts": [
        {
            "matches": [
                "https://www.facebook.com/*"
            ],
           "js": [
                "content.js"
            ],
            "run_at": "document_end"
        }
    ]
}

//content.js //content.js

document.body.innerHTML = document.body.innerHTML.replace(new RegExp("the", "g"), "teh");

I have, for her sake, removed the actual name and replaced it with the and teh. 为了她的缘故,我已经删除了实际名称,并用和替换了它。 Even doing this it doesn't work. 即使这样做也行不通。 According to every place I check this should work , but it doesn't, and I don't know why. 根据我检查过的每个地方,它应该可以工作 ,但是不能,而且我也不知道为什么。 I am mostly oing this to brush up on JS and gradually make more and more elaborate extensions for myself, but I'm pretty frustrated at this right now. 我主要是想以此来复习JS并逐步为自己做越来越复杂的扩展,但是现在我对此感到非常沮丧。 Can anyone help me with this? 谁能帮我这个?

Its not incorrect, but this won't work for content that loaded after document_end which is most of the content on facebook ( it is dynamically loaded ). 它不是不正确的,但这不适用于document_end之后加载的内容,它是facebook上的大部分内容(它是动态加载的)。 Try replacing your name and it should work. 尝试替换您的名字,它应该可以工作。

I tried it in my extension and it is showing intended behaviour . 我在扩展程序中尝试过它,它显示了预期的行为。

To change dynamically loaded content, you will have to call it after every content load. 要更改动态加载的内容,必须在每次加载内容后调用它。 Or to keep it simple ( but inefficient ) you can use javascript setInterval to call the replace periodically. 为了保持简单(但效率低下),您可以使用javascript setInterval定期调用replace。

ref : http://www.w3schools.com/jsref/met_win_setinterval.asp 参考: http : //www.w3schools.com/jsref/met_win_setinterval.asp

EDIT : 编辑:

There are other ways but my prefered way is using mutation observers, 还有其他方法,但我更喜欢的方法是使用变异观察器,

    var obs = new MutationObserver(function(mutations, observer) {
        for(var i=0; i<mutations.length; ++i) {

            //dom mutation observed, do your replace here

            for(var j=0; j<mutations[i].addedNodes.length; ++j) {

                     // every mutated dom node triggers 
                     // this area so you can check for 
                     // specific case when news feed etc are 
                     // mutated to make it even more sleek
                }
            }
        }
    });

    obs.observe( target, params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试让jQuery在我的chrome扩展程序的background.js中工作,并且它将无法工作。 为什么? - I'm trying to get jquery working in the background.js of my chrome extension and it won't work. why? 我正在尝试在文本区域中进行特定的输入,调用特定的javascript函数,但是它将无法正常工作 - I'm trying to make a specific input into a text area call a specific javascript function, but it won't work Chrome扩展程序不适用于HTTPS网址 - Chrome extension won't work on HTTPS urls 使用javascript查找和替换迭代 - find-and-replace iteration with javascript 我无法在Chrome扩展程序中执行“命令” - I cannot make “commands” in chrome extension work 我正在尝试为我的 class 编写一个简单的密码验证系统,但它不起作用 - I'm trying to write a simple password validation system for my class but it won't work 为什么我的.vue文件找不到我要传递的JSON? - Why won't my .vue file find the JSON I'm trying to pass it? 跨域AJAX GET Chrome扩展无法使用! - Cross Domain AJAX GET Chrome Extension won't work! 我正在尝试在Chrome扩展程序中获取某个URL的cookie - I'm trying to get a certain URL's cookies in a chrome extension Javascript查找和替换特殊字符 - Javascript find-and-replace special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM