同步获取内容脚本中的存储数据 [英] Synchronously get Stored data from content scripts

查看:85
本文介绍了同步获取内容脚本中的存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发Chrome扩展程序,源代码为在Github上可用。目标是将自定义JavaScript注入到网页中。



目前我将每个自定义Javascript Inject 存储在 localStorage 中,并从 content scipts 。我已将 run_at 设置为 document_start



我使用它来从后台脚本中获取存储的注入

<$ p $函数(注入){
for(inject中的索引){
if(/ ^ items \。){code> chrome.extension.sendMessage({method:get_injects /.test(index)){
itemJSON = injects [index];
//if(windowow.location.host.toString()。indexOf(itemJSON.url)){
if (window.location.host.toString()。match(itemJSON.url +'$')){
var js = itemJSON.js.toString();
eval(js);
}
}
}
});



问题



我想运行注入脚本 准确地,然后加载文档。但是使用呈现的方法,控制将传递 chrome.extension.sendMessage ,并且不会等待获取响应注入。因此, eval(js); 将在页面加载过程中执行。如何解决这个问题,以便我们能够在页面加载之前注入?

它自己的过程。后台页面在扩展进程中运行,与您正在挂钩的选项卡不同。所以我恐怕没有真正的同步解决方案。



即使使用 chrome.storage (它没有原始限制,因此不需要后台页面)将是异步的。



但是这里有一个适用于大多数页面的解决方案:如果您需要的数据位于页面的localStorage中,请转至6

  • 删除页面上的所有内容

  • 当您拥有数据时,对数据进行异步请求

  • 将其存储在页面的localStorage中(而不是后台)
  • 使用相同的URL刷新页面

  • 利润


  • I am currently working on a chrome extension and source code is available on Github. The goal is to inject custom Javascript into webpages.

    Currently I store each custom Javascript Inject in localStorage and call them from content scipts. I have set run_at to be at document_start.

    I use this, to get stored injects from background script:

    chrome.extension.sendMessage({method:"get_injects"},function(injects){
        for(index in injects){
            if(/^items\./.test(index)){
                itemJSON = injects[index];
                //if(window.location.host.toString().indexOf(itemJSON.url)){
                if(window.location.host.toString().match(itemJSON.url + '$')){
                    var js = itemJSON.js.toString();
                    eval(js);
                }
            }
        }
    });
    

    Problem

    I want to run Inject scripts exactly before document loads. But using presented method, control will pass chrome.extension.sendMessage and wont wait to get responseinjects. So, eval(js); will be executed at the middle of page loading. How can I solve this, so that I will be able to inject before page loading?

    解决方案

    In Chrome, each tab runs in its own process. The background page runs in the extension process which is different from the one of the tab you're hooking. So I'm afraid there can be no really synchronous solution.

    Even using chrome.storage (which hasn't the origin limitation and thus doesn't need the background page) would be asynchronous.

    But here's a solution that would work for most pages :

    1. if the data you need is in the localStorage of the page, go to 6
    2. remove everything from the page
    3. do your asynchronous request for data
    4. when you have the data, store it in the localStorage of the page (not the background one)
    5. refresh the page with same URL
    6. profit

    这篇关于同步获取内容脚本中的存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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