vem*_*emv 7 javascript bookmarklet
我的bookmarklet包含对插入的"启动器"脚本的调用body.在运行时,它以类似的方式插入更多脚本(jQuery,实际应用程序)和CSS.
小书签
javascript:(function(){
var l = document.createElement('script');
l.setAttribute('type','text/javascript');
l.setAttribute('src','launcher.js');
document.body.appendChild(l);
})();
Run Code Online (Sandbox Code Playgroud)
Launcher.js
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src','my actual script');
document.body.appendChild(s);
// I repeat a block like this once per script/css.
Run Code Online (Sandbox Code Playgroud)
问题是如果单击两次书签,将再次插入所有脚本.我该如何防止这种行为?
您可以在window名称奇怪的对象上设置一个属性,因此仅在第一次运行时不太可能导致命名空间冲突。在后续运行中,如果该属性存在,则代码将不会执行:
javascript:(function(){
// Test for existence of your weird property
if (!window.hasOwnProperty('yourWeirdPropertyX774x9ZZYy4')) {
// Run all your existing code
var l = document.createElement('script');
l.setAttribute('type','text/javascript');
l.setAttribute('src','launcher.js');
document.body.appendChild(l);
// Set the flag so this won't run again
window.yourWeirdPropertyX774x9ZZYy4 = true;
}
})();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
570 次 |
| 最近记录: |