メインコンテンツへスキップ
見出し画像

不動産ポータルサイト(SUUMO・athome・ふれんず)に坪単価を追記するブックマークレットを作ってみた。

    タイトルの通りですが、不動産ポータルサイト(SUUMO・athome・ふれんず)に坪単価を追記するブックマークレットを作ってみました。実装したのは私ではなく全てAIです。

    設定方法は簡単で、ブラウザのブックマークのアドレス欄に下記 javascript をコピペして一覧や詳細ページでクリックするだけです。

    また、Tampermonkey 等のブラウザの拡張機能を利用すればデフォルト表示として設定することもできます。
    現在、SUUMO・athome・ふれんずの一覧と詳細画面に対応しています。
    Mac の Chrome と Android の Firefox では動作確認しました。動作環境によっては対応していないのでご了承下さい。

    ブックマークレット

    javascript:(()=>{const e="tsubo-shared-style";if(!document.getElementById(e)){const t=document.createElement("style");t.id=e,t.textContent=".tb{display:block;margin-top:4px;padding:2px 6px;border-radius:4px;font-weight:600;font-size:.82em;line-height:1.4;background:rgba(255,165,0,.16);color:#b45309}.tb-s{background:rgba(59,130,246,.18);color:#1d4ed8}.tb-a{background:rgba(244,114,182,.2);color:#be185d}",document.head.appendChild(t)}const t=3.305785,r=e=>e?e.replace(/\s+/g," ").trim():"",n=e=>{if(!e)return null;const t=e.replace(/\s+/g,"");let r=0;const n=t.match(/([\d.,]+)億/);n&&(r+=1e8*parseFloat(n[1].replace(/,/g,"")));const o=t.match(/([\d.,]+)万/);if(o&&(r+=1e4*parseFloat(o[1].replace(/,/g,""))),!n&&!o){const e=t.replace(/[^\d]/g,"");e&&(r+=parseInt(e,10))}return r||null},o=e=>{if(!e)return null;const r=e.replace(/㎡/g,"m2").replace(/m²/g,"m2");let n=null;const o=r.match(/([\d.,]+)m2/i);o&&(n=parseFloat(o[1].replace(/,/g,"")));let l=null;const a=r.match(/([\d.,]+)\s*坪/);return a&&(l=parseFloat(a[1].replace(/,/g,""))),!l&&n&&(l=n/t),l&&!n&&(n=l*t),l?{sqm:n,tsubo:l}:null},l=(e,t,n)=>{for(const o of e){if(!r(o.textContent).includes(t))continue;let e=n(o);if(e)return{element:e,text:r(e.textContent)}}return null},a=(e,t)=>{if(!e)return null;const n=Array.isArray(t)?t:[t];for(const t of n){const r=l(e.querySelectorAll("td.category"),t,e=>{let t=e.nextElementSibling;for(;t&&"TD"===t.tagName&&!t.classList.contains("value");)t=t.nextElementSibling;return t});if(r)return r}for(const t of n){const r=l(e.querySelectorAll("dt"),t,e=>{let t=e.nextElementSibling;for(;t&&"DD"!==t.tagName;)t=t.nextElementSibling;return t});if(r)return r}for(const t of n){const r=l(e.querySelectorAll("th"),t,e=>{let t=e.nextElementSibling;for(;t&&"TD"!==t.tagName;)t=t.nextElementSibling;return t});if(r)return r}for(const t of n)for(const n of e.querySelectorAll("span,strong,div,p,dd,td,li")){const e=r(n.textContent);if(e&&e.includes(t))return{element:n,text:e}}return null},c=(e,t,r,n)=>{if(!e)return;let o=e;if(n&&e.closest){const t=e.closest(n);t&&(o=t)}if(o.querySelector&&o.querySelector(":scope > .tb"))return;if(o.style){o.style.whiteSpace="normal";"inline"===getComputedStyle(o).display&&(o.style.display="inline-block")}const l=document.createElement("div");l.className=r,l.textContent=(t>=100?t.toFixed(0):t.toFixed(1))+"万円/坪",o.appendChild(l)},s=/(?:^|\.)athome\.co\.jp$/.test(location.hostname),i=s?document.querySelector("athome-csite-pc-property-detail-ryutsu-sell-living, athome-csite-sp-property-detail-ryutsu-sell-living"):null,u=!!(s?document.querySelector("athome-csite-pc-property-list-sell-living, athome-csite-sp-property-list-sell-living"):null)&&!i,p=({items:e,priceLabels:t,areaLabels:l,className:s,containerSelector:i,fallbackMap:p})=>{if(u&&"tb"===s)return!1;if(!e.length)return!1;let d=!1;return e.forEach(e=>{if("1"===e.dataset.tbBadgeInjected)return;if(e.querySelector(".tb"))return;const u=e.querySelector(".tb-host")||e;let b=a(e,t),f=a(e,l);const m=e.querySelector(".bukken-cassette__kakaku"),y=e.querySelector(".bukken-cassette__menseki");!b&&m&&(b={element:m,text:r(m.textContent)}),!f&&y&&(f={element:y,text:r(y.textContent)});let k=n(b&&b.text),g=o(f&&f.text);if((!k||!g)&&p){const t=e.querySelector("input[id]")?.id||e.querySelector("[data-bukken-no]")?.getAttribute("data-bukken-no");if(t&&p.has(t)){const e=p.get(t);!k&&e.price&&(k=e.price),!g&&e.area&&(g=e.area)}}if(!k||!g)return;const S=k/g.tsubo/1e4,h=b&&b.element||f&&f.element||u,q=h.closest('.property-price, .property-info__price, [class*="price"], .tb-host')||h;q&&q.querySelector(".tb")||(c(h,S,s,i),e.dataset.tbBadgeInjected="1",d=!0)}),d},d=e=>{const t=[];return e.forEach(e=>{e&&document.querySelectorAll(e).forEach(e=>t.push(e))}),[...new Set(t)]},b=({priceNodes:e,areaRoot:t,area:l,className:s,fallbackPrice:i})=>{if(!e.length)return!1;let u=l;if(!u){const e=a(t,["専有面積","面積"]);u=o(e&&e.text)}if(!u)return!1;let p=!1;return e.forEach(e=>{if(e.querySelector(".tb"))return;const t=n(r(e.textContent))||i;if(!t)return;const o=t/u.tsubo/1e4;e.style&&(e.style.whiteSpace="normal","inline"===getComputedStyle(e).display&&(e.style.display="inline-block")),c(e,o,s,null),p=!0}),p},f=new Map;let m=null,y=null;const k=document.querySelector("#serverApp-state");if(k)try{const e=JSON.parse(k.textContent||"{}"),r=Object.keys(e).find(e=>e.includes("/bukken/list/"));if(r&&e[r]&&e[r].body){const t=JSON.parse(e[r].body).data?.bukkenData?.bukkenList;Array.isArray(t)&&t.forEach(e=>{const t=e?.bukkenNo;if(!t)return;const r=n(e?.kakaku?e.kakaku+"万円":null);let l=null;e?.areaInfo&&(l=o(e.areaInfo.tsubo||e.areaInfo.area)),f.set(t,{price:r,area:l})})}const l=Object.keys(e).find(e=>e.includes("property-detail-ryutsu"));if(l&&e[l]&&e[l].body){const r=JSON.parse(e[l].body).data,n=r?.propertyData?.areaInfo,o=r?.propertyData?.contract?.price?.price;if(n)if(n.tsubo){const e=parseFloat(n.tsubo.replace(/[^\d.]/g,""));Number.isNaN(e)||(m={tsubo:e,sqm:n.area&&parseFloat(n.area.replace(/[^\d.]/g,""))||e*t})}else if(n.area){const e=parseFloat(n.area.replace(/[^\d.]/g,""));Number.isNaN(e)||(m={sqm:e,tsubo:e/t})}if(o){const e=Number(o);Number.isNaN(e)||(y=e)}}}catch(e){console.warn("athome state parse failed",e)}p({items:[...document.querySelectorAll("li.item.list-tpl, li.item.smartphone-tpl, [component-bukken-list-item], athome-csite-sp-part-bukken-card-ryutsu-sell-living")],priceLabels:["価格"],areaLabels:["専有面積","面積"],className:"tb",containerSelector:"td.value,dd",fallbackMap:f}),p({items:[...document.querySelectorAll("#js-bukkenList .property_unit, .bukken-cassette__body")],priceLabels:["販売価格","価格"],areaLabels:["専有面積","面積"],className:"tb tb-s",containerSelector:"dd,td.value,.dottable-value,.property_unit-info,.bukken-cassette__kakaku,.bukken-cassette__menseki",fallbackMap:f});const g=[...document.querySelectorAll(".card-box")];g.length&&g.forEach(e=>{if("1"===e.dataset.tbBadgeInjected)return;if(e.querySelector(".tb"))return;const t=e.querySelector(".property-price"),l=[...e.querySelectorAll(".property-detail-table__block")].find(e=>{const t=e.querySelector("strong");return t&&r(t.textContent).includes("専有面積")}),a=l&&l.textContent;let s=n(t&&t.textContent),i=o(a);if((!s||!i)&&f.size){const t=e.querySelector("input[id]")?.id||e.querySelector("[data-bukken-no]")?.getAttribute("data-bukken-no");if(t&&f.has(t)){const e=f.get(t);!s&&e.price&&(s=e.price),!i&&e.area&&(i=e.area)}}s&&i&&t&&(c(t,s/i.tsubo/1e4,"tb tb-a",null),e.dataset.tbBadgeInjected="1")});if(!s||!!i){const e=["span.price-area"];s&&e.push("p.price-main"),b({priceNodes:d(e),areaRoot:document,className:"tb"});const t=s?document.querySelector(".property-summary__list .rent"):null;if(!y&&t){const e=n(r(t.textContent));e&&(y=e)}const l=[document.querySelector(".futureInfo"),document.querySelector(".detail-sum-table"),document.querySelector(".property-summary__list"),document.querySelector(".bukken-detail__table"),document.querySelector("[component-page__basic-info-table]"),document];let i=null;for(const e of l){if(!e)continue;const t=a(e,["専有面積","面積"]);if(t&&o(t.text)){i=e;break}}let u=null;const p=document.querySelector(".futureInfo_menseki_disp");if(u=o(p&&p.textContent),!u&&i){const e=a(i,["専有面積","面積"]);u=o(e&&e.text)}if(!u){const e=a(document,["専有面積"]);u=o(e&&e.text)}if(m&&(u=m),u){const e=["p.mt7.b",".bukken-detail__price",".bukken-detail__price-value",".futureInfo_list.futureInfo_kakaku",".futureInfo .card-price","[component-property-info-header] .card-price"];s&&e.push(".property-summary__list .rent","p.price-main");if(b({priceNodes:d(e),areaRoot:i||document,area:u,className:"tb tb-s",fallbackPrice:y}),s&&y&&u.tsubo){const e=y/u.tsubo/1e4,t=[".property-detail-top-area .card-price",".property-info__price",'.property-detail-top-area [class*="price"]',".main-contents__box .card-price"];t.unshift("p.price-main");const r=d(t);let n=!1;for(const t of r)if(t){if(t.querySelector(".tb")){n=!0;break}if(c(t,e,"tb tb-a",null),t.querySelector(".tb")){n=!0;break}}if(!n){const t=document.querySelector(".property-detail-top-area")||document.body,r=document.createElement("div");r.style.marginTop="8px",t.insertBefore(r,t.firstChild),c(r,e,"tb tb-a",null)}}}}})();

    コードも公開しています。

    ご自由に改変して下さい。
    Pull Requestも歓迎です。私も気が向いたら改良します。

    以下、ブックマークレットの動作参考例です。

    SUUMO

    SUUMO PC Chrome 一覧画面

    画像
    SUUMO PC Chrome 一覧画面

    SUUMO PC Chrome 詳細画面

    画像
    SUUMO PC Chrome 詳細画面

    SUUMO Android Firefox 一覧画面

    画像
    SUUMO Android Firefox 一覧画面

    SUUMO Android Firefox 詳細画面

    画像
    SUUMO Android Firefox 詳細画面

    athome

    athome PC Chrome 一覧画面

    画像
    athome PC Chrome 一覧画面

    athome PC Chrome 詳細画面

    画像
    athome PC Chrome 詳細画面

    athome Android Firefox 一覧画面

    画像
    athome Android Firefox 一覧画面

    athome Android Firefox 詳細画面

    画像
    athome Android Firefox 詳細画面

    ふれんず

    ふれんず PC Chrome 一覧画面

    画像
    ふれんず PC Chrome 一覧画面

    ふれんず PC Chrome 詳細画面

    画像
    ふれんず PC Chrome 詳細画面

    ふれんず Android Firefox 一覧画面

    画像
    ふれんず Android Firefox 一覧画面

    ふれんず Android Firefox 詳細画面

    画像
    ふれんず Android Firefox 詳細画面


    これは便利!中古マンションの価格比較が捗ります。
    AIに頼めばサクッと作ってくれるので、他にもお気に入りのサイトがあれば、AIに作ってもらいましょう。
    しかし福岡市のマンションの坪単価も高くなりましたね…。


    尚、本件はKou-san氏の「坪レンズ」というアプリを知り、PC や Android でも見たいと思い、作ってみました。iOSの方はこちらも是非ご覧下さい。


    今後もnoteで記事を更新していきます。
    少しでも気に入っていただけたら、「スキ」やフォローで応援していただけると、大変励みになります!

    あなたへのおすすめ