Skip to content

Instantly share code, notes, and snippets.

@ajhsu
Created April 1, 2023 11:40
Show Gist options
  • Save ajhsu/217d6598d1bbb8cd081c22299f1b6fac to your computer and use it in GitHub Desktop.
Save ajhsu/217d6598d1bbb8cd081c22299f1b6fac to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name 592
// @version 0.1
// @match https://rent.591.com.tw/*
// ==/UserScript==
(function () {
'use strict';
// Check if the current URL is in the array
if (window.location.href.indexOf('https://rent.591.com.tw/home/') === 0) {
const postId = document.querySelector('#hid_postId').value;
const posts = JSON.parse(localStorage.getItem('posts')) || [];
if (posts.includes(postId)) {
// If the current URL is in the array, apply the "visited" class to the body element
document.body.classList.add('visited');
} else {
// Add the current URL to the array
posts.push(postId);
// Save the updated array back to localStorage
localStorage.setItem('posts', JSON.stringify(posts));
}
}
if (window.location.href.indexOf('https://rent.591.com.tw/') === 0) {
checkForMoreSections().then(() => {
const posts = JSON.parse(localStorage.getItem('posts')) || [];
const items = document.querySelectorAll('section.vue-list-rent-item');
items.forEach((item) => {
const pId = item.dataset.bind;
if (posts.includes(pId)) {
const isCollected = item
.querySelector('div.item-collect')
.classList.contains('active');
if (isCollected) {
item.style = 'background:lightyellow;';
} else {
item.style = 'filter:brightness(0.4);background:darkgray;';
}
}
getObjectData(pId).then((objectData) => {
console.log(pId, objectData);
const visitorCount = objectData.data.browse;
const favCount = objectData.data.favData.count;
item.querySelector(
'div.item-title'
).textContent += `(收藏 ${favCount} / 瀏覽 ${visitorCount.pc} / ${visitorCount.mobile})`;
const postTime = objectData.data.publish.postTime;
// const postTime = new Date(objectData.data.favData.posttime * 1000);
item.querySelector('div.item-msg').textContent += `/ ${postTime}`;
const extraCostElem = document.createElement('span');
const extraCost = objectData.data.costData.data.find(
(_costData) => _costData.key === 'manageprice'
);
if (extraCost) {
extraCostElem.className = 'item-price-text';
extraCostElem.textContent = `管理費 ${extraCost.value}`;
item.querySelector('div.item-price').appendChild(extraCostElem);
}
});
});
});
}
function checkForMoreSections() {
return new Promise(function (resolve, reject) {
var intervalId = setInterval(function () {
var sections = document.querySelectorAll('section.vue-list-rent-item');
if (sections.length > 1) {
clearInterval(intervalId);
console.log('Item loaded');
resolve();
}
}, 1000); // Interval time in milliseconds
});
}
async function getObjectData(pid) {
return await fetch(
`https://corsproxy.io/?https://bff.591.com.tw/v1/house/rent/detail?id=${pid}`,
{
headers: {
accept: '*/*',
'accept-language': 'en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7',
'cache-control': 'no-cache',
device: 'pc',
pragma: 'no-cache',
'sec-ch-ua':
'"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
},
referrer: 'https://rent.591.com.tw/',
referrerPolicy: 'strict-origin-when-cross-origin',
body: null,
method: 'GET',
mode: 'cors',
credentials: 'omit',
}
).then((r) => r.json());
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment