Skip to content

Commit

Permalink
#3 新マイページ対応
Browse files Browse the repository at this point in the history
  • Loading branch information
sakasa committed Aug 18, 2023
1 parent ca3a3d9 commit 1bc1894
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 36 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Chrome extension for attendance chat post with `https://biz.moneyforward.com/att
[マネーフォワードクラウド勤怠](https://biz.moneyforward.com/attendance) のホーム画面にある出勤・退勤等の打刻のアクションでチャットツールに投稿を行うGoogleChrome拡張機能です。

以下の環境で確認しています。
- [Google Chrome] バージョン: 84.0.4147.105
- [Google Chrome] バージョン: 84.0.4147.105


## Install
Expand Down Expand Up @@ -45,21 +45,15 @@ pageUrl: {
```json
"content_scripts": [
{
"matches": ["https://attendance.moneyforward.com/my_page"],
"matches": ["https://*.moneyforward.com/*"],
"js": ["contentScript.js"]
}
],
```


## その他
Chrome拡張機能のオプションページとポップアップページのcssに [Bootstrap5](https://v5.getbootstrap.jp/docs/5.0/migration/) を使っています。
```html
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous">
```
バージョンがAlphaなので突然画面が崩れたりするかもしれません。
...

## Blog
https://note.com/ppiicckkllees/n/n97674a7bf2a1

12 changes: 10 additions & 2 deletions attendance-ext/background.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
hostEquals: 'attendance.moneyforward.com',
pathEquals: '/my_page'
},
})],
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
hostEquals: 'mypage.moneyforward.com',
pathEquals: '/'
},
}),
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
Expand Down
117 changes: 94 additions & 23 deletions attendance-ext/contentScript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const sleep = waitTime => new Promise( resolve => setTimeout(resolve, waitTime) );

let user = '';
let chatConf = {};

Expand All @@ -10,8 +12,6 @@ function loadChatConf(){
}
});
};
loadChatConf();
// console.log(chatConf);

function getUserText() {
// console.log('getUserText')
Expand Down Expand Up @@ -40,27 +40,66 @@ function getUserText() {
// console.log(user);
return user;
}

function getDateText() {
// console.log('getDateText')
const statusContainer = document.getElementsByClassName('status-container')[0];
const dateStr = statusContainer.firstChild.firstChild.firstChild.innerText;
const timeStr = statusContainer.lastChild.firstChild.firstChild.innerText;
const datetime = '[' + dateStr + ' ' + timeStr + ']';
// console.log(datetime);
return datetime;
const dateStr = statusContainer.firstChild.firstChild.firstChild.innerText.replaceAll(' ', '');
const timeStr1 = statusContainer.lastChild.firstChild.firstChild.innerText;
const timeStr2 = statusContainer.lastChild.firstChild.lastChild.innerText;
const ret = `[${dateStr} ${timeStr1}:${timeStr2}]`;
// console.log(ret);
return ret;
}
function getMessageText(text) {
// console.log(text);
return `${user||getUserText()} - ${getDateText()} ${text}`
}

function dataJson(text){
/**
* for `mypage.moneyforward.com`
*/
function getUserText2() {
const ret = document.getElementById('root').firstChild.firstChild.lastChild.lastChild.firstChild.firstChild.innerText;
// console.log(ret);
return ret;
}
/**
* for `mypage.moneyforward.com`
*/
function getDateText2() {
let ret = '';
document.getElementsByTagName('attendance-time-record-container')[0].shadowRoot.childNodes.forEach(function(element) {
// console.log(element.tagName);
if (element.tagName === 'SECTION') {
const dateStr = element.getElementsByClassName('status-container')[0].firstChild.getElementsByTagName('section')[0].innerText.replaceAll(' ', '');
const timeStr1 = element.getElementsByClassName('status-container')[0].lastChild.firstChild.firstChild.innerText;
const timeStr2 = element.getElementsByClassName('status-container')[0].lastChild.firstChild.lastChild.innerText;
ret = `[${dateStr} ${timeStr1}:${timeStr2}]`;
}
});
// console.log(ret);
return ret;
}
/**
* for `mypage.moneyforward.com`
*/
function getMessageText2(text) {
// console.log(text);
return `${user||getUserText2()} - ${getDateText2()} ${text}`
}

function dataJson(messageText){
// console.log(messageText);

let ret = {
"text" : `${user||getUserText()} - ${getDateText()} ${text}`,
"text" : messageText,
"username": (chatConf.username || null) ?? "kintai-bot"
};
if(chatConf.channel) {
ret.channel = chatConf.channel;
}
// console.log(ret);

return ret;
}

Expand All @@ -83,16 +122,51 @@ function postChat(data){
});
}

const timeStampButtons = document.getElementsByClassName('time-stamp-button');
for(let i=0;i<timeStampButtons.length; i++){
const element = timeStampButtons[i];
element.addEventListener('click', function(evt){
const data = dataJson(element.innerText);
// alert(data);
postChat(data);
});
function chatPostByClick(messageText) {
// console.log(messageText);
const data = dataJson(messageText);
// console.log(data);
postChat(data);
}

(async function() {
/* load configration */
loadChatConf();
// console.log(chatConf);

/* event */
const chatPostEventName = 'click';

/*
* for `attendance.moneyforward.com`
*/
if (location.hostname === 'attendance.moneyforward.com' && location.pathname === '/my_page') {
[...document.getElementsByClassName('time-stamp-button')].forEach(function(element) {
element.addEventListener(chatPostEventName, function(e) {
chatPostByClick(getMessageText(element.innerText));
});
});
}

/*
* for `mypage.moneyforward.com`
*/
if (location.hostname === 'mypage.moneyforward.com' && location.pathname === '/') {
await sleep(1000);
document.getElementsByTagName('attendance-time-record-container')[0].shadowRoot.childNodes.forEach(
function(element) {
if (element.tagName === 'SECTION') {
[...element.getElementsByClassName('time-stamp-button')].forEach(function(elem) {
elem.addEventListener(chatPostEventName, function(e) {
chatPostByClick(getMessageText2(elem.innerText));
});
});
}
}
);
}
})();

chrome.storage.onChanged.addListener(function(changes, namespace) {
for (let key in changes) {
const storageChange = changes[key];
Expand All @@ -101,11 +175,8 @@ chrome.storage.onChanged.addListener(function(changes, namespace) {
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
storageChange.newValue
);
}
loadChatConf();
});

// document.addEventListener('click', function(e) {
// console.log(e);
// })
4 changes: 2 additions & 2 deletions attendance-ext/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Kintai Post with Moneyforward Attendance",
"version": "0.1.3",
"version": "0.2.0",
"description": "「マネーフォワード クラウド勤怠」の打刻に合わせて、設定したチャットに投稿を行います。チャットの設定は拡張機能のオプションページで行ってください。",
"permissions": [
"declarativeContent",
Expand All @@ -21,7 +21,7 @@
},
"content_scripts": [
{
"matches": ["https://attendance.moneyforward.com/my_page"],
"matches": ["https://*.moneyforward.com/*"],
"js": ["contentScript.js"]
}
],
Expand Down

0 comments on commit 1bc1894

Please sign in to comment.