Javascript Bookmarklet 在 IE8 中失败

发布于 2024-08-24 13:56:46 字数 950 浏览 4 评论 0原文

有人想知道为什么这个小书签在 IE8 中失败吗?它将 Friefox 和 Safari 中的所有文本转为大写。但在 IE8 中,它只是因“正在加载...”而停止。我在 IE8 中启用了运行 scriplet 并降低了 javascript 安全设置。

更新 2010 年 3 月 19 日: 此书签现在可以在 IE 中运行,感谢 sergey。

javascript:
(function(){
  var i,t,D=document,h,f,g=D.getElementsByTagName,ce=D.createElement,c=D.createStyleSheet,ac='appendChild',cn='childNodes',l,s='*{text-transform:uppercase}input,textarea{text-transform:none}';
  for(i=0;t=document.getElementsByTagName('textarea')[i];i++)
    t.value=t.value.toUpperCase();
  if(D.namespaces){
    h=g('head')[0];
    f=h[ac](ce('div'));
    f.innerHTML='b<style type=\'text/css\'>'+s+'</style>';
    h[ac](f[cn][1]);
    f.parentNode.removeChild(f);
  }
  else
    if(c){
      c('javascript:\''+s+'\'')
    }
  else{
    l=document.createElement('link');
    l.rel='stylesheet';
    l.href='data:text/css,'+escape(s);
    D.documentElement[cn][0][ac](l)
  }
}
)()

Anyone want to take a stab at why this bookmarklet fails in IE8? It turns all text uppercase in Friefox and Safari. But in IE8, it simply stalls with "loading..." I've enabled running scriplets and lowered javascript security settings in IE8.

Update 3/19/10: This bookmarklet now works in IE, thanks to sergey.

javascript:
(function(){
  var i,t,D=document,h,f,g=D.getElementsByTagName,ce=D.createElement,c=D.createStyleSheet,ac='appendChild',cn='childNodes',l,s='*{text-transform:uppercase}input,textarea{text-transform:none}';
  for(i=0;t=document.getElementsByTagName('textarea')[i];i++)
    t.value=t.value.toUpperCase();
  if(D.namespaces){
    h=g('head')[0];
    f=h[ac](ce('div'));
    f.innerHTML='b<style type=\'text/css\'>'+s+'</style>';
    h[ac](f[cn][1]);
    f.parentNode.removeChild(f);
  }
  else
    if(c){
      c('javascript:\''+s+'\'')
    }
  else{
    l=document.createElement('link');
    l.rel='stylesheet';
    l.href='data:text/css,'+escape(s);
    D.documentElement[cn][0][ac](l)
  }
}
)()

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

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

发布评论

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

评论(2

Smile简单爱 2024-08-31 13:56:46

对代码进行少量添加即可达到目的:

javascript:(function(){
   var i,t,D=document;
  for(i=0;t=D.getElementsByTagName('textarea')[i];++i)
    t.value=t.value.toUpperCase();
   var newSS,styles='*{text-transform:uppercase}input,textarea{text-transform:none}';
  if (document.namespaces) {
    var head = document.getElementsByTagName("head")[0];
    var factory = head.appendChild(document.createElement("div"));
    factory.innerHTML = ' <style type=\'text/css\'>' + styles + '</style>';
    head.appendChild(factory.childNodes[1]);
    factory.parentNode.removeChild(factory);
  }
  else
  if(D.createStyleSheet){
    D.createStyleSheet("javascript:'"+styles+"'");
  }
  else{
    newSS=D.createElement('link');
    newSS.rel='stylesheet';
    newSS.href='data:text/css,'+escape(styles);
    D.documentElement.childNodes[0].appendChild(newSS);
  }
}
)()

更新 17/03/10:我缩小了代码,创建了一个书签并在 IE 中成功使用了它。

2010 年 3 月 18 日更新:我注意到缩小后的代码无法在 IE 浏览器以外的其他浏览器中运行,这是经过测试的版本:

javascript:(function(){var i,t,D=document,h,f,g=D.getElementsByTagName,ce=D.createElement,c=D.createStyleSheet,ac='appendChild',cn='childNodes',l,s='*{text-transform:uppercase}input,textarea{text-transform:none}';for(i=0;t=document.getElementsByTagName('textarea')[i];i++)t.value=t.value.toUpperCase();if(D.namespaces){h=g('head')[0];f=h[ac](ce('div'));f.innerHTML='b<style type=\'text/css\'>'+s+'</style>';h[ac](f[cn][1]);f.parentNode.removeChild(f);}else if(c){c('javascript:\''+s+'\'')}else{l=document.createElement('link');l.rel='stylesheet';l.href='data:text/css,'+escape(s);D.documentElement[cn][0][ac](l)}})()

Small addition to your code will do the trick:

javascript:(function(){
   var i,t,D=document;
  for(i=0;t=D.getElementsByTagName('textarea')[i];++i)
    t.value=t.value.toUpperCase();
   var newSS,styles='*{text-transform:uppercase}input,textarea{text-transform:none}';
  if (document.namespaces) {
    var head = document.getElementsByTagName("head")[0];
    var factory = head.appendChild(document.createElement("div"));
    factory.innerHTML = ' <style type=\'text/css\'>' + styles + '</style>';
    head.appendChild(factory.childNodes[1]);
    factory.parentNode.removeChild(factory);
  }
  else
  if(D.createStyleSheet){
    D.createStyleSheet("javascript:'"+styles+"'");
  }
  else{
    newSS=D.createElement('link');
    newSS.rel='stylesheet';
    newSS.href='data:text/css,'+escape(styles);
    D.documentElement.childNodes[0].appendChild(newSS);
  }
}
)()

Update 17/03/10: I minified the code, created a bookmarklet out of and successfully used it in IE.

Update 18/03/10: I noticed the minified code did not run in other than IE browsers, here is the tested version:

javascript:(function(){var i,t,D=document,h,f,g=D.getElementsByTagName,ce=D.createElement,c=D.createStyleSheet,ac='appendChild',cn='childNodes',l,s='*{text-transform:uppercase}input,textarea{text-transform:none}';for(i=0;t=document.getElementsByTagName('textarea')[i];i++)t.value=t.value.toUpperCase();if(D.namespaces){h=g('head')[0];f=h[ac](ce('div'));f.innerHTML='b<style type=\'text/css\'>'+s+'</style>';h[ac](f[cn][1]);f.parentNode.removeChild(f);}else if(c){c('javascript:\''+s+'\'')}else{l=document.createElement('link');l.rel='stylesheet';l.href='data:text/css,'+escape(s);D.documentElement[cn][0][ac](l)}})()
空城仅有旧梦在 2024-08-31 13:56:46

作为参考,这是“未缩小”的代码:

javascript:(function () {
var i, t, D = document;
for (i = 0; t = D.getElementsByTagName('textarea')[i]; ++i) {
    t.value = t.value.toUpperCase();
    var newSS, styles = '* { text-transform: uppercase; } input, textarea { text-transform: none}';
    if (D.createStyleSheet) {
        D.createStyleSheet("javascript:'" + styles + "'");
    } else {
        newSS = D.createElement('link');
        newSS.rel = 'stylesheet';
        newSS.href = 'data:text/css,' + escape(styles);
        D.documentElement.childNodes[0].appendChild(newSS);
    }
}
})()

我的 Mac 上没有方便的 IE 8,但尝试撒入 alert('here at line XXX'); 以便穷人进行调试。

也许 IE 8 不喜欢样式表的 javascript: 源?

此外,循环会添加与文本区域一样多的样式表。那太愚蠢了。将其移出循环,如下所示:

javascript:(function () {
var i, t, D = document;
for (i = 0; t = D.getElementsByTagName('textarea')[i]; ++i) {
    t.value = t.value.toUpperCase();
}
var styles = '* { text-transform: uppercase; } input, textarea { text-transform: none}';
if (D.createStyleSheet) {
    D.createStyleSheet("javascript:'" + styles + "'");
} else {
    var newSS = D.createElement('link');
    newSS.rel = 'stylesheet';
    newSS.href = 'data:text/css,' + escape(styles);
    D.documentElement.childNodes[0].appendChild(newSS);
}
})()

请注意,该代码中仍然存在一些有问题的内容。我不太确定该循环的条件。我要做的就是将其从循环中取出,如下所示:

javascript:(function () {
var D = document;
var i, t = D.getElementsByTagName('textarea');
for (i = 0; i < t.length; i++) {
    t[i].value = t[i].value.toUpperCase();
}
/* Rest of the code here. */
})()

作为读者/询问者的练习:这仅是大写文本区域,而不是文本输入。

编辑
缩小它以保持在最大长度以下:

javascript:(function () {
var D = document;
var i, t = D.getElementsByTagName('textarea');
for (i = 0; i < t.length; i++) {
    t[i].value = t[i].value.toUpperCase();
}
D.createStyleSheet('javascript:"* { text-transform: uppercase; } input, textarea { text-transform: none}"');
})()

结果:

javascript:(function(){var D = document;var i,t=D.getElementsByTagName('textarea');for(i=0;i<t.length;i++)t[i].value=t[i].value.toUpperCase();D.createStyleSheet('javascript:"*{text-transform:uppercase;}input,textarea{text-transform:none}"');})()

这只有 IE 特定的代码。

For reference, this is the "unminified" code:

javascript:(function () {
var i, t, D = document;
for (i = 0; t = D.getElementsByTagName('textarea')[i]; ++i) {
    t.value = t.value.toUpperCase();
    var newSS, styles = '* { text-transform: uppercase; } input, textarea { text-transform: none}';
    if (D.createStyleSheet) {
        D.createStyleSheet("javascript:'" + styles + "'");
    } else {
        newSS = D.createElement('link');
        newSS.rel = 'stylesheet';
        newSS.href = 'data:text/css,' + escape(styles);
        D.documentElement.childNodes[0].appendChild(newSS);
    }
}
})()

I do not have IE 8 handy on my Mac, but try sprinkling in an alert('here at line XXX'); for poor man's debugging.

Maybe IE 8 does not like a javascript: source for the style sheet?

Also, the loop is adding as many style sheets as there are textareas. That is silly. Move it out of the loop, like so:

javascript:(function () {
var i, t, D = document;
for (i = 0; t = D.getElementsByTagName('textarea')[i]; ++i) {
    t.value = t.value.toUpperCase();
}
var styles = '* { text-transform: uppercase; } input, textarea { text-transform: none}';
if (D.createStyleSheet) {
    D.createStyleSheet("javascript:'" + styles + "'");
} else {
    var newSS = D.createElement('link');
    newSS.rel = 'stylesheet';
    newSS.href = 'data:text/css,' + escape(styles);
    D.documentElement.childNodes[0].appendChild(newSS);
}
})()

There are still some questionable things in that code, mind you. I am not too sure of that loop's condition. What I would do, is get it out of the loop, like this:

javascript:(function () {
var D = document;
var i, t = D.getElementsByTagName('textarea');
for (i = 0; i < t.length; i++) {
    t[i].value = t[i].value.toUpperCase();
}
/* Rest of the code here. */
})()

As an exercise to the reader/asker: this only uppercases textareas, not text inputs.

EDIT:
Minify this to stay below the maximum length:

javascript:(function () {
var D = document;
var i, t = D.getElementsByTagName('textarea');
for (i = 0; i < t.length; i++) {
    t[i].value = t[i].value.toUpperCase();
}
D.createStyleSheet('javascript:"* { text-transform: uppercase; } input, textarea { text-transform: none}"');
})()

Result:

javascript:(function(){var D = document;var i,t=D.getElementsByTagName('textarea');for(i=0;i<t.length;i++)t[i].value=t[i].value.toUpperCase();D.createStyleSheet('javascript:"*{text-transform:uppercase;}input,textarea{text-transform:none}"');})()

This only has the IE-specific code.

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