简体   繁体   English

尽快将变量从chrome扩展程序传递到网页

[英]Pass variable to webpage from chrome extension as soon as possible

I am trying to detect if my chrome extension is installed using js executed on my website. 我正在尝试检测是否使用在我的网站上执行的js安装了chrome扩展程序。 I have found some examples, some I don't even understand or could make them work. 我发现了一些示例,有些我什至不理解,或者可能使它们起作用。 What I tried and kinda worked is adding elements to DOM or modifying some css property. 我尝试过的工作是在DOM中添加元素或修改某些CSS属性。 For example: 例如:

On my extension background page I have: 在我的扩展程序后台页面上,我有:

function changeBG(tab){
     chrome.tabs.executeScript(tab.id, {
      code: ' document.body.bgColor="red" '
     });
}

chrome.tabs.onUpdated.addListener(changeBG);

On my website I have: 在我的网站上,我有:

<script>
  alert(document.body.bgColor);
</script>

The background does turn red, but .bgColor is null because I try to read it on the top of my website code as soon as the page starts loading. 背景确实变成红色,但是.bgColor为null,因为一旦页面开始加载,我就会尝试在网站代码的顶部读取它。 After the page loaded I can read it and it returns "red". 页面加载后,我可以阅读它并返回“红色”。 (same thing happen if injecting an element into the page and then trying to read innerHTML or a property of the element). (如果在页面中注入元素,然后尝试读取innerHTML或元素的属性,也会发生同样的事情)。

But I don't want to wait for onload or anything, because my content depends on if the extension is installed or not. 但是我不想等待onload或其他任何东西,因为我的内容取决于是否安装了扩展程序。

I have also tried the method of loading an image from the extension content but once again this is an indirect ¿asynchronous? 我还尝试了从扩展内容中加载图像的方法,但再次说明这是间接的“异步? method. 方法。

I don't want to "swap" the content, I want to know if the extension is installed before continuing to load anything else on my website . 我不想“交换”内容, 我想知道是否已安装扩展程序,然后再继续在我的网站上加载其他内容

Does someone knows how I could do this? 有人知道我该怎么做吗?

As I understand, javascript check is executed too late for you, because webpage content is already processed by a browser. 据我了解,JavaScript检查对您来说为时已晚,因为网页内容已由浏览器处理。 If you want to know if extension is enabled before any content is loaded you should check it on the server side. 如果要在加载任何内容之前知道扩展是否已启用,则应在服务器端进行检查。 The way to do this is to make your extension to append some signature to headers sent by browser to servers. 这样做的方法是使扩展程序在浏览器发送到服务器的标头上附加一些签名。 Then, if signature is present, you will know that request sender has your extension installed and you will be able to return proper content. 然后,如果存在签名,您将知道请求发件人已安装了扩展程序,并且能够返回适当的内容。 Header manipulation is described here . 标头操作在此处介绍。 You will probably need to use this: chrome.webRequest.onBeforeSendHeaders.addListener . 您可能需要使用: chrome.webRequest.onBeforeSendHeaders.addListener

Since it is your own website use the chrome.app.isInstalled method used for inline installation . 由于这是您自己的网站, chrome.app.isInstalled使用用于内联安装chrome.app.isInstalled方法。 You will have to verify your domain with Webmaster Tools first. 您必须先使用网站站长工具验证您的域。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM