0

I love to pimp my Chrome/Chromium experience with JavaScript/Bookmarklets, but now i am facing the following problem:

The code

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";

Explained

  1. var src=prompt("src of <img> to float",document.querySelector("img").getAttribute('src')); src attribute of first <img>
  2. scr.length>0? if NOT NULL
  3. var ThisDOM=src: ThisDOM = prompted value, else
  4. ThisDOM="Alap/Alap00.gif", default, most used settings
  5. document.querySelector("img[src*='"+ThisDOM+"']"). select the desired <img>
  6. style.cssText= add desired css style
  7. "position:fixed;left:0;top:0;max-height:200px;box-shadow: 1px 1px 46px -21px;opacity:0.9"; css style settings

The problem

  • [✔] Running the code from DevTools>Console apply the css style settings to the image properly,
  • [ø] but running from the Bookmarklet just printing out the css style settings (Explained at 7.)
eapo
  • 1,053
  • 1
  • 19
  • 40

1 Answers1

0

During the process of beautifying my question above i tried wrapping the code in a function as described here: javascript - function as google chrome bookmark and finally did work!

The wrapper

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

Final code

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";})();

But i am still curious why didn't work without the function wrapper?

eapo
  • 1,053
  • 1
  • 19
  • 40