繁体   English   中英

已启用选项的内容脚本Chrome扩展程序没有后台页面?

[英]Options-enabled content-script Chrome extension without background page?

我正在为Google Chrome制作内容脚本扩展程序,它会为网站的页面添加功能。 我想添加几个选项,真的不大,我只需要两个字符串(其中没有一个是敏感的用户数据)。

这个答案 ,我认为我需要一个背景页面,我宁愿不添加到我的扩展 - 我不希望它增加不必要的重量。

我真的需要一个背景页面,或者我可以有一个没有它的选项页面(我可以使用哪个存储空间)?

UPDATE
从Chrome 20开始,您现在可以使用Storage api .....
http://code.google.com/chrome/extensions/storage.html

老路
我所做的是创建一个指向我的扩展中的页面的iframe,该页面具有从本地存储获取我需要的设置的脚本,然后在内容脚本获得的消息中将其发送到其父级.....这是一个垃圾解释,代码说它更好;).......

内容脚本

// create the iframe for our page that sends the settings
var el = document.createElement("iframe");
el.setAttribute('src', chrome.extension.getURL("gimmeSettings.html"));
el.style.visibility="hidden";
document.body.appendChild(el);

// create the listner that listens for a message from our page that sends the settings
window.addEventListener("message", receiveSettings, false);

// function that gets called when we recieve a message from the page that sends the settings  
function receiveSettings(event) {
        //check to make sure the message came from our page
        if (event.origin !== "chrome-extension://"+chrome.i18n.getMessage("@@extension_id")) return;

        //message came from our extension, do stuff with it  
        console.debug(event.data);

        // clean up
        window.removeEventListener("message", receiveSettings, false);
        el.parentNode.removeChild(el);
}

gimmeSettings.html的JS

// post the message with our settings
parent.postMessage( localStorage.getItem("testing"), "*" );

Options.html的JS

localStorage.setItem("testing","bleh");

表现

{
    "name": "Getting at an extensions local storage from a content script",
    "description" : "Getting at an extensions local storage from a content script.  Be aware that other pages/extensions can use this to get at your settings, but not change them...so dont include sensitvie data.",
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js" : ["myscript.js"],
            "run_at":"document_idle"
        }
    ],
    "permissions": [
        "tabs", "<all_urls>"
    ],
    "manifest_version": 2,
    "web_accessible_resources": [
    "gimmeSettings.html"
  ],
  "options_page": "options.html",
    "version":"1.0"
}

有些事要注意......
其他页面和扩展可以很容易地使用它来从您的扩展中获取设置,因此不要使用此方法的任何敏感数据。
从最好的我可以告诉他们没有办法通过该页面改变你的设置,如果有人知道不同请解释。
我使用清单版本2并将页面gimmeSettings设置为可访问。 如果你不知道差异清单版本2添加你真的应该阅读它.... http://code.google.com/chrome/extensions/trunk/manifestVersion.html

如果你想要一个有效的例子,那就去这里.....
http://forum.valorsolo.com/viewtopic.php?f=36&t=375

我只是有一个想法,但我不知道它是否合理或有意义。

我相信我可以从我的content_script访问HTML5 localStorage ,并在那里存储两个字符串。 通过消息传递,我应该能够告诉content_script其中一个更改(从选项页面),然后让它更新其localStorage

这是唯一的选择吗? 如果有效的话听起来不太糟糕......

暂无
暂无

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

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