【问题标题】:Script running from Console but printing variables from Bookmarklet?从控制台运行脚本但从 Bookmarklet 打印变量?
【发布时间】:2020-01-22 10:38:27
【问题描述】:

我喜欢用JavaScript/Bookmarklets 来提升我的 Chrome/Chromium 体验,但现在我面临以下问题:

代码

javascript:/*img2float*/ var str=prompt("src of img to float",document.querySelector("img").getAttribute('src'));str.length>0?content=str:content="Alap/Alap00.gif",document.querySelector("img[src*='"+content+"']").style.cssText="position:fixed;left:0;top:0;max-height:200px;box-shadow: 1px 1px 46px -21px;opacity:0.9";

解释

  1. var src=prompt("src of <img> to float",document.querySelector("img").getAttribute('src')); src 第一个 属性
  2. scr.length>0? 如果 不为空
  3. var ThisDOM=src: ThisDOM = 提示值, else
  4. ThisDOM="Alap/Alap00.gif", 默认,最常用的设置
  5. document.querySelector("img[src*='"+ThisDOM+"']"). 选择想要的
  6. style.cssText= 添加所需的 css 样式
  7. "position:fixed;left:0;top:0;max-height:200px;box-shadow: 1px 1px 46px -21px;opacity:0.9";css 样式设置

问题

  • [✔] 从 DevTools>Console 运行代码,将 css 样式设置正确应用到图像,
  • [ø] 但从 Bookmarklet 运行只是打印出 css 样式 settings(解释于 7 .)

【问题讨论】:

    标签: javascript css google-chrome chromium bookmarklet


    【解决方案1】:

    在美化我上面的问题的过程中,我尝试将代码包装在一个函数中,如下所述:javascript - function as google chrome bookmark,终于成功了!

    包装器

    javascript:(function(){ })();
    

    最终代码

    javascript:/*img2float*/ (function(){var scr=prompt("src of img to float",document.querySelector("img").getAttribute('src'));scr.length>0?ThisDOM=scr:ThisDOM="Alap/Alap00.gif",document.querySelector("img[src*='"+ThisDOM+"']").style.cssText="position:fixed;left:0;top:0;max-height:200px;box-shadow: 1px 1px 46px -21px;opacity:0.9";})();
    

    但我还是很好奇为什么没有函数包装器就不能工作?

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多