我的Chrome扩展程序有一个设置页面。在设置页面中,我想要预览和保存按钮。我希望能够将临时选项对象传递给我的APPLICATION。我怎么能这样做,以便我不必重写两次APPLICATION?如何将信息传递给html页面,这次它应该访问LocalStorage ['permanent_settings']和其他LocalStorage ['temporary_settings'],并使用该对象作为设置对象呈现内容。此外,我希望我的代码在本地执行,所以我不想要任何PHP等。
答案 0 :(得分:2)
您可以将此条件添加到此方法,该方法允许您动态添加外部JavaScript文件:
loadExternalScriptFile = function(filename) {
var fileref = document.createElement("script");
if (fileref){
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref);
}
}
//dynamically load and add this .js file
loadExternalScriptFile("myscript.js");
答案 1 :(得分:1)
您应该尝试将读取源保存在本地存储中,方式与:
类似// At startup, defaulting to permanent_settings
function onLoad() {
LocalStorage['read_from'] = 'permanent_settings';
// Other initialization work
// ...
}
// When pressing Save
function onSaveClick() {
LocalStorage['read_from'] = 'permanent_settings';
}
// When pressing Preview
function onPreviewClick() {
LocalStorage['read_from'] = 'temporary_settings';
}
// When accessing the settings, read their source
function getSettingForKey(var key) {
var source = LocalStorage['read_from'];
// It can be either permanent_settings, or temporary_settings
var settingsArray = LocalStorage[source];
return settingsArray[key];
}
答案 2 :(得分:0)
您可以使用jQuery加载脚本,甚至可以在加载脚本时执行代码。您甚至不必将代码包装在$(function(){ ... })
块中:
$.getScript(chrome.extension.getURL("myscript.js"), function() {
alert("myscript.js has finished loading");
});