Quantcast
Channel: CSSタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8567

スマホ用のJavascript/CSSコンソールを作ったお話

$
0
0

スマホ版ブラウザでPC版Chromeコンソールのようなものが(即座に!)使えればいいのになと思ったので、非常に簡易的なものではありますが、作りました。

ブックマークレットのコード

bookmarklet
javascript:window.outerJsConsole%3D%7Be%3Afunction(v%2Cf)%7Bvara%3Ddocument.createElement(v)%3Bif(v%3D%3D"script")%7Ba.setAttribute("type"%2C"text%2Fjavascript")%3Bif(!f)%7Ba.setAttribute("id"%2C"newScriptTag")%3B%7Da.classList.add("OJCscript")%3B%7Delseif(v%3D%3D"style")%7Ba.setAttribute("type"%2C"text%2Fcss")%3Bf%3Fa.setAttribute("id"%2C"baseStyleTag")%3Aa.classList.add("OJCstyle")%3B%7Delse%7Ba.classList.add("OJCelm")%3B%7Dreturna%3B%7D%2Calert%3Afunction(v%2Ccaution)%7Bvara%3DnewDate()%2Cah%3Da.getHours()<10%3F"0"%2Ba.getHours()%3Aa.getHours()%2Cam%3Da.getMinutes()<10%3F"0"%2Ba.getMinutes()%3Aa.getMinutes()%2Cas%3Da.getSeconds()<10%3F"0"%2Ba.getSeconds()%3Aa.getSeconds()%2Cb%3Dthis.now%3F"(JS)"%3A"(CSS)"%2Cc%3Dcaution%3F%60class%3D"OJCcaution"%60%3A""%2Cd%3D%60<table%60%2Bc%2B%60><tbody><tr><tdstyle%3D"font-style%3A italic%3Bwidth%3A 6em">%60%2Bah%2B"%3A"%2Bam%2B"%3A"%2Bas%2B%60<br><bstyle%3D"color%3A %23aaa">%60%2Bb%2B%60<%2Fb><%2Ftd><td><prestyle%3D"background%3A transparent%3Bborder%3A none%3Bborder-radius%3A 0%3Bfont-family%3A sans-serif">%60%2Bv%2B%60<%2Fpre><%2Ftd><%2Ftr><%2Ftbody><%2Ftable>%60%2Ce%3Ddocument.getElementById("OJCalert")%3Be.innerHTML%3Dd%2Be.innerHTML%3B%7D%2Carea%3Afunction()%7Bvara%3Dthis.e("style"%2Ctrue)%2Cb%3Dthis.e("div")%3Ba.innerHTML%3D%60%23OJCconsole%23OJCalert%7Bfont-family%3Asans-serif%3B%7D%23OJCconsole%23OJCalert%7Bposition%3Afixed%3Bbackground%3A%23f0f0f0%3Bborder%3A1pxsolid%23aaa%3Bbottom%3A30vh%3Bfont-size%3A0.9em%3Bleft%3A0%3Bpadding%3A0.75em%3Bheight%3A5rem%3Bwidth%3A100%25%3Boverflow-y%3Ascroll%3B-webkit-overflow-scrolling%3Atouch%3B%7D%23OJCconsole%23OJCalerttable%7Bborder-bottom%3A1pxsolid%23aaa%3Bdisplay%3Ablock%3Bpadding%3A0.5em0%3B%7D%23OJCconsole%23OJCalerttable.OJCcaution%7Bbackground%3A%23fff0f0%3B%7D%23OJCconsole.OJCwrap%7Bposition%3Afixed%3Bbottom%3A0%3Bleft%3A0%3Bheight%3A30vh%3Bwidth%3A100%25%3B%7D%23OJCconsole.OJCwrap*%7Bposition%3Arelative%3Bappearance%3Anone%3B-webkit-appearance%3Anone%3B-moz-appearance%3Anone%3B%7D%23OJCconsole.OJCwraptextarea%7Bborder%3A1pxsolid%23aaa%3Bborder-radius%3A0%3Bfont-size%3A16px%3Btransform%3Ascale(0.8)%3Btransform-origin%3Atopleft%3Bheight%3Acalc((100%25-4em)%2F0.8)%3Bpadding%3A0.75em%3Bwidth%3Acalc(100%25%2F0.8)%3B-webkit-overflow-scrolling%3Atouch%3B%7D%23OJCconsole.OJCwrapdiv%7Bdisplay%3Aflex%3Bheight%3A3em%3Bjustify-content%3Acenter%3B%7D%23OJCconsole.OJCwrapdivbutton%7Bbackground%3A%23b01%3Bborder%3A1pxsolid%23fff%3Bcolor%3A%23fff%3Bdisplay%3Ablock%3Bflex-grow%3A1%3Bfont-family%3Asans-serif%3Bfont-size%3A15px%3Bfont-weight%3Abold%3Bpadding%3A0.5em%3Bwidth%3A15%25%3B%7D%60%3Bb.setAttribute("id"%2C"OJCconsole")%3Bb.innerHTML%3D%60<divid%3D"OJCalert"><%2Fdiv><divclass%3D"OJCwrap"><div><buttononclick%3D"outerJsConsole.change(this)%3B"style%3D"background%3A %23065fd4%3B">JS<%2Fbutton><buttononclick%3D"outerJsConsole.apply()%3B">apply<%2Fbutton><buttononclick%3D"outerJsConsole.code()%3B">code<%2Fbutton><buttononclick%3D"outerJsConsole.close()%3B">close<%2Fbutton><%2Fdiv><textareaid%3D"outerJsTgt"><%2Ftextarea><%2Fdiv>%60%3Bdocument.head.appendChild(a)%3Bdocument.body.appendChild(b)%3B%7D%2Cnow%3Atrue%2Cchange%3Afunction(v)%7Bvara%3D(b%2Cc)%3D>%7Bthis.now%3Db%3Bv.innerHTML%3Dc%3B%7D%3Bthis.now%3Fa(false%2C"CSS")%3Aa(true%2C"JS")%3B%7D%2Capply%3Afunction()%7Bvarx%3Ddocument.querySelectorAll(".OJCscript%2C.OJCstyle").length%2Ca%3Ddocument.getElementById("outerJsTgt")%3Btry%7Bvarb%3Ddocument.getElementById("newScriptTag")%3Bb.removeAttribute("id")%3B%7Dcatch(e)%7B%7Dvarc%3Dthis.now%3Fthis.e("script")%3Athis.e("style")%2Cd%3Da.value.replace(%2F%5E%5Cn%2B%7C%5Cn%2B%24%2Fg%2C"")%3Bc.innerHTML%3Dd%3Bdocument.head.appendChild(c)%3Bvary%3Ddocument.querySelectorAll(".OJCscript%2C.OJCstyle").length%3Bif(x!%3D%3Dy)%7Bthis.alert(d)%3B%7D%7D%2Ccode%3Afunction()%7Bvara%3Dthis.now%3Fdocument.querySelectorAll(".OJCscript")%3Adocument.querySelectorAll(".OJCstyle")%3Bif(a.length%3D%3D0)%7Breturnthis.alert("No code.")%3B%7Dvarb%3D""%3Bfor(vari%3D0%3Bi<a.length%3Bi%2B%2B)%7Bb%2B%3Da%5Bi%5D.innerHTML%2B"%5Cn"%3B%7Dreturnthis.alert(b)%3B%7D%2Cclose%3Afunction()%7Bwindow.onerror%3D""%3Bvara%3Ddocument.querySelectorAll(".OJCelm%2C%23baseStyleTag")%3Bfor(vari%3D0%3Bi<a.length%3Bi%2B%2B)%7Ba%5Bi%5D.parentNode.removeChild(a%5Bi%5D)%3B%7Dwindow.outerJsConsole%3D%7B%7D%3B%7D%2Cinit%3Afunction()%7Bwindow.onerror%3Dfunction(msg%2Curl%2Clinenumber)%7BouterJsConsole.alert(msg%2B"(at%3A "%2Blinenumber%2B")."%2Ctrue)%3Bvara%3Ddocument.getElementById("newScriptTag")%3Ba.parentNode.removeChild(a)%3Breturntrue%3B%7D%3Bthis.area()%3Bthis.now%3Dtrue%3B%7D%7D%3BouterJsConsole.init()%3Bvoid(0);

以上のコードをコピペして登録してください。コピペの際には周りの文章ごと選択してメモ帳などで余分な部分を消すと楽です。iPhoneから見るとコードが全部表示されてしまうようですね…。なんてこった…。

q-1-1.png

ブックマークレットの登録方法は以下のページが詳しいですので、参考までに。

ブックマークレットの登録方法:
https://qiita.com/aqril_1132/items/b5f9040ccb8cbc705d04

見た目/できること

q-1-2.pngq-1-3.png
寿司

ブックマークレットを起動すると、上記のようなコンソールが現れます。

各ボタンの概要:

  • 青いボタン
    • 「JS(JavaScript)」モードと「CSS」モードを切り替えます。
  • apply
    • テキストエリア内のコードを適用します。
  • code
    • これまでに適用したコードを全てリザルトに表示します。
  • close
    • コンソールを閉じます。

JS(JavaScript)モード

特に説明することは無いです。PC版Chromeコンソールのようにオブジェクトの中身を覗いたりといったことは出来ず、ただスクリプトをそのページに追加するだけです。

ただしそのスクリプトに問題があったときは、以下のような警告をリザルトに表示します。

q-1-3.png

この時、そのスクリプトの適用はされません。

Tips:オブジェクトの中身を見たい場合は、alertやJSON.stringify()などを駆使すると良いです。

JavaScript
alert(JSON.stringify(<Object>));

CSSモード

CSSを変更できます。シンプルですね。例えばqiita(このサイト)で使用する場合、

CSS
.it-MdContent{color: red;}

このCSSを適用することで、記事の本文を真っ赤に染め上げることができます。

最後に

ブックマークレットに加工する前のコードを以下に載せます。

JavaScript
window.outerJsConsole={e: function(v,f){vara=document.createElement(v);if(v=="script"){a.setAttribute("type","text/javascript");if(!f){a.setAttribute("id","newScriptTag");}a.classList.add("OJCscript");}elseif(v=="style"){a.setAttribute("type","text/css");f?a.setAttribute("id","baseStyleTag"):a.classList.add("OJCstyle");}else{a.classList.add("OJCelm");}returna;},alert: function(v,caution){vara=newDate(),ah=a.getHours()<10?"0"+a.getHours():a.getHours(),am=a.getMinutes()<10?"0"+a.getMinutes():a.getMinutes(),as=a.getSeconds()<10?"0"+a.getSeconds():a.getSeconds(),b=this.now?"(JS)":"(CSS)",c=caution?` class="OJCcaution"`:"",d=`<table`+c+`><tbody><tr><td style="font-style: italic; width: 6em">`+ah+":"+am+":"+as+`<br><b style="color: #aaa">`+b+`</b></td><td><pre style="background: transparent;border: none;border-radius: 0;font-family: sans-serif">`+v+`</pre></td></tr></tbody></table>`,e=document.getElementById("OJCalert");e.innerHTML=d+e.innerHTML;},area: function(){vara=this.e("style",true),b=this.e("div");a.innerHTML=`
        #OJCconsole #OJCalert {
            font-family: sans-serif;
        }
        #OJCconsole #OJCalert {
            position: fixed;
            background: #f0f0f0;
            border: 1px solid #aaa;
            bottom: 30vh;
            font-size: 0.9em;
            left: 0;
            padding: 0.75em;
            height: 5rem;
            width: 100%;
            overflow-y: scroll;
                -webkit-overflow-scrolling: touch;
        }
        #OJCconsole #OJCalert table {
            border-bottom: 1px solid #aaa;
            display: block;
            padding: 0.5em 0;
        }
        #OJCconsole #OJCalert table.OJCcaution {
            background: #fff0f0;
        }
        #OJCconsole .OJCwrap {
            position: fixed;
            bottom: 0;
            left: 0;
            height: 30vh;
            width: 100%;
        }
        #OJCconsole .OJCwrap * {
            position: relative;
            appearance: none;
                -webkit-appearance: none;
                -moz-appearance: none;
        }
        #OJCconsole .OJCwrap textarea {
            border: 1px solid #aaa;
            border-radius: 0;
            font-size: 16px;
            transform: scale(0.8);
            transform-origin: top left;
            height: calc((100% - 4em) / 0.8);
            padding: 0.75em;
            width: calc(100% / 0.8);
                -webkit-overflow-scrolling: touch;
        }
        #OJCconsole .OJCwrap div {
            display: flex;
            height: 3em;
            justify-content: center;
        }
        #OJCconsole .OJCwrap div button {
            background: #b01;
            border: 1px solid #fff;
            color: #fff;
            display: block;
            flex-grow: 1;
            font-family: sans-serif;
            font-size: 15px;
            font-weight: bold;
            padding: 0.5em;
            width: 15%;
        }`;b.setAttribute("id","OJCconsole");b.innerHTML=`<div id="OJCalert"></div><div class="OJCwrap"><div><button onclick="outerJsConsole.change(this);" style="background: #065fd4;">JS</button><button onclick="outerJsConsole.apply();">apply</button><button onclick="outerJsConsole.code();">code</button><button onclick="outerJsConsole.close();">close</button></div><textarea id="outerJsTgt"></textarea></div>`;document.head.appendChild(a);document.body.appendChild(b);},now: true,change: function(v){vara=(b,c)=>{this.now=b;v.innerHTML=c;};this.now?a(false,"CSS"):a(true,"JS");},apply: function(){varx=document.querySelectorAll(".OJCscript, .OJCstyle").length,a=document.getElementById("outerJsTgt");try{varb=document.getElementById("newScriptTag");b.removeAttribute("id");}catch(e){}varc=this.now?this.e("script"):this.e("style"),d=a.value.replace(/^\n+|\n+$/g,"");c.innerHTML=d;document.head.appendChild(c);vary=document.querySelectorAll(".OJCscript, .OJCstyle").length;if(x!==y){this.alert(d);}},code: function(){vara=this.now?document.querySelectorAll(".OJCscript"):document.querySelectorAll(".OJCstyle");if(a.length==0){returnthis.alert("No code.");}varb="";for(vari=0;i<a.length;i++){b+=a[i].innerHTML+"\n";}returnthis.alert(b);},close: function(){window.onerror="";vara=document.querySelectorAll(".OJCelm,#baseStyleTag");for(vari=0;i<a.length;i++){a[i].parentNode.removeChild(a[i]);}window.outerJsConsole={};},init: function(){window.onerror=function(msg,url,linenumber){outerJsConsole.alert(msg+" (at: "+linenumber+").",true);vara=document.getElementById("newScriptTag");a.parentNode.removeChild(a);returntrue;};this.area();this.now=true;}};outerJsConsole.init();

リザルト用のtableに直接styleを付けたりしていて汚いですが、各サイト毎のCSSに対応する&文字数削減のためのものですので、ご了承ください。


Viewing all articles
Browse latest Browse all 8567

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>