有条件地加载 JavaScript 脚本
我有一个带有设置页面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 jQuery 加载脚本,甚至在加载脚本时执行代码。您甚至不必将代码包装在
$(function(){ ... })
块中: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:您可以将条件添加到此方法中,这样您就可以动态添加外部 javascript 文件:
You can add your conditions to this method, which allows you to dynamically add an external javascript file:
您还应该尝试将读取源保存在本地存储中,方式类似于:
You should try holding the read source in local storage as well, in a similar manner to: