在Chrome扩展程序中,您可以强制在一切之前注入一些javascript吗? [英] In Chrome extensions, can you force some javascript to be injected before everything?

查看:187
本文介绍了在Chrome扩展程序中,您可以强制在一切之前注入一些javascript吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Chrome扩展程序(下面提供的源代码),它会遇到竞争条件。我需要一些注入的JavaScript才能在网页上的所有其他JavaScript之前运行。

I have a Chrome extension (source provided below) that is getting caught with a race condition. I need some injected JavaScript to run before all other JavaScript on a web page.

我想要做的一个简单示例的源代码是:< a href =https://github.com/nddipiazza/oogi =nofollow noreferrer> https://github.com/nddipiazza/oogi

The source code of a simple example of what I'm trying to do is here: https://github.com/nddipiazza/oogi

它正在尝试将名称空间添加到实际将作为cookie保留的所有cookie名称,但同时从正在使用的cookie中删除这些名称空间。

It is attempting to add a namespace to all cookie names that will actually be persisted as cookies, but at the same time remove those namespaces from the cookies that are in use.

因此,假设没有扩展名,通常会在访问网站后保存2个Cookie:

So let's say normally without the extension you would have 2 cookies that are saved after accessing a site:

JSESSIONID
lastVisit

此扩展名将其保存为:

oogi$JSESSIONID
oogi$lastVisit

扩展基本上有两个主要部分。

There are basically two major parts to the extension.


  • < a href =https://github.com/nddipiazza/oogi/blob/master/background.js =nofollow noreferrer> https://github.com/nddipiazza/oogi/blob/master/background.js
    这会拦截传入和传出的http标头,以便它可以正确地将命名空间添加到传入的cookie中,并从传出的cookie中删除它们。这样,cookie的名称空间就完全是我们本地的。

  • https://github.com/nddipiazza/oogi/blob/master/background.js This intercepts the incoming and outgoing http headers so that it can properly add the namespace to the incoming cookies and remove the ones from the outgoing. This way the namespace of the cookie is purely local to us.

https://github.com/nddipiazza/oogi/blob/master/inject.js
这拦截了JavaScript cookie获取和设置操作我们为标题做的原因相同。

https://github.com/nddipiazza/oogi/blob/master/inject.js This intercepts JavaScript cookie get and set operations for the same reason as we do it for the headers.

这里的问题是,为了使这个工作,我需要javascript cookie标头在 inject.js 中截获,绝对总是在任何其他javascript之前加载。但事实并非如此。

The problem here is that in order for this to work, I need the javascript cookie header intercepts in inject.js to absolutely always be loaded before any other javascript. But it doesn't.

示例:内联javascript中的Cookie,例如:

Example: Cookies in inline javascript such as:

<body>
<H2>Cookies from Inline JavaScript</H2>
<script>
    console.log("Inline javascript is executed.");
    document.write(listCookies());
</script>
</body>

在加载注入cookie拦截器之前已经加载了。您可以从控制台日志中看出将按以下顺序读取:

Will already load prior to the inject cookie interceptor being loaded. You can tell from the console log will read in this order:

Inline javascript is executed.
cookie get/set injector completed

有没有办法解决此注入竞争条件?我想允许Chrome扩展程序强制在页面上执行任何 javascript之前运行javascript。

Is there a way to fix this inject race condition? I want to allow a chrome extension to force a javascript to be run prior to doing any javascript on a page.

推荐答案

感谢这张票的评论,我的案例中的解决方案是答案的方法2:https://stackoverflow.com/a/9517879

Thanks to the comments on this ticket, the solution in my case was Method 2 from this answer: https://stackoverflow.com/a/9517879

特别感谢https://stackoverflow.com/users/3959875/woxxom

这是完成解决方案的链接:

Here is a link to the finished solution:

https ://github.com/nddipiazza/oogi/commit/64e1ef8dc3abfb32fec2db5fb67891a29cfe12ea

代码的重要部分就是这里

The important part of the code that makes the difference is here

var actualCode = `var cookieGetter = document.__lookupGetter__("cookie").bind(document);
var cookieSetter = document.__lookupSetter__("cookie").bind(document);
var getPrefix = function() {
    return "oogi$"
};
var processCookieStr = function(cookiesStr) {
    var prefix = getPrefix();
    var cookieStrList = cookiesStr.split('; ');
    var newStrList = [];
    cookieStrList.forEach(function(cookieStr){
        if (cookieStr.indexOf(prefix)==0) {
            newStrList.push(cookieStr.substring(prefix.length, cookieStr.length));
        }
    });
    return newStrList.join("; ");
};
var processSetCookieStr = function(str) {
    console.log("Processing set cookie string " + str);
    return getPrefix()+str;
};
Object.defineProperty(document, 'cookie', {
    get: function() {
        var storedCookieStr = cookieGetter();
        console.log("Intercepted a cookie get " + storedCookieStr + " , and returning processed cookie string " + processCookieStr(storedCookieStr));
        return processCookieStr(storedCookieStr);
    },
    set: function(cookieString) {
        var newValue = processSetCookieStr(cookieString);
        console.log("Intercepted a cookie set " + newValue)
        return cookieSetter(newValue);
    }
});
console.log("cookie get/set injector completed");
`;

var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();

这篇关于在Chrome扩展程序中,您可以强制在一切之前注入一些javascript吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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