有条件地加载 JavaScript 脚本

发布于 2024-12-03 19:35:42 字数 241 浏览 1 评论 0原文

我有一个带有设置页面的 Chrome 扩展。在设置页面中,我想要有预览和保存按钮。我希望能够将临时选项对象传递给我的应用程序。我怎样才能做到这一点,这样我就不必重写应用程序两次?如何将信息传递到 html 页面,这次应该访问 LocalStorage['permanent_settings'] 和其他 LocalStorage['temporary_settings'],并使用该对象作为设置对象呈现内容。另外我希望我的代码在本地执行,所以我不需要任何 PHP 等。

I have a chrome extension that have a settings page. In the settings page I want to have preview and save buttons. I want to be able to pass temporary options object to my APPLICATION. How can I do it so that i don't have to rewrite APPLICATION twice? How to pass information to html page that this time it should access LocalStorage['permanent_settings'] and other LocalStorage['temporary_settings'], and render content using that object as a settings object. Also I want my code to execute locally, so I don't want any PHP etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一桥轻雨一伞开 2024-12-10 19:35:43

您可以使用 jQuery 加载脚本,甚至在加载脚本时执行代码。您甚至不必将代码包装在 $(function(){ ... }) 块中:

$.getScript(chrome.extension.getURL("myscript.js"), function() {
    alert("myscript.js has finished loading");
});

You can use jQuery to load the script and even execute code when it's loaded. You don't even have to wrap the code in a $(function(){ ... }) block:

$.getScript(chrome.extension.getURL("myscript.js"), function() {
    alert("myscript.js has finished loading");
});
鹊巢 2024-12-10 19:35:42

您可以将条件添加到此方法中,这样您就可以动态添加外部 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");     

You can add your conditions to this method, which allows you to dynamically add an external javascript file:

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");     
假扮的天使 2024-12-10 19:35:42

您还应该尝试将读取源保存在本地存储中,方式类似于:

// 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];
}

You should try holding the read source in local storage as well, in a similar manner to:

// 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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文