forked from daiiz/ScrapScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
553 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,101 @@ | ||
// background | ||
import browser from "webextension-polyfill"; | ||
|
||
const isChrome = () => { | ||
return /Chrome/.test(navigator.userAgent) | ||
} | ||
browser.runtime.onMessage.addListener( | ||
( | ||
/** @type {{ command: "enable-daiiz-script" | "get-project-name"|"get-clipboard-page"|"fetch-page-title"; func_project_pairs: Record<string,string>; func_names: any; rawText: any; }} */ request, | ||
/** @type {any} */ _sender, | ||
/** @type {{ (arg0: {}): void; (arg0: any): void; (arg0: any): void; }} */ sendResponse, | ||
) => { | ||
const cmd = request.command; | ||
|
||
window.app = isChrome() ? chrome : browser | ||
// 外部サイトで発動する機能を有効にする | ||
if (cmd === "enable-daiiz-script") { | ||
const funcProjectPairs = request.func_project_pairs; | ||
|
||
window.app.runtime.onMessage.addListener(function (request, sender, sendResponse) { | ||
var cmd = request.command; | ||
const funcNames = Object.keys(funcProjectPairs); | ||
for (const funcName of funcNames) { | ||
const projectName = funcProjectPairs[funcName]; | ||
|
||
// 外部サイトで発動する機能を有効にする | ||
if (cmd === 'enable-daiiz-script') { | ||
var funcProjectPairs = request.func_project_pairs; | ||
if (!funcName || funcName.length === 0) { | ||
return; | ||
} | ||
if (!projectName || projectName.length === 0) { | ||
localStorage.removeItem(funcName); | ||
} else if (projectName.length > 0) { | ||
localStorage[funcName] = projectName; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
var funcNames = Object.keys(funcProjectPairs); | ||
for (var i = 0; i < funcNames.length; i++) { | ||
var funcName = funcNames[i]; | ||
var projectName = funcProjectPairs[funcName]; | ||
// 設定された値を返す | ||
if (cmd === "get-project-name") { | ||
const funcNames = request.func_names; | ||
/** @type {Record<string, string>} */ | ||
const projectNames = {}; | ||
for (const funcName of funcNames) { | ||
if (localStorage[funcName]) { | ||
projectNames[funcName] = localStorage[funcName]; | ||
} | ||
} | ||
sendResponse(projectNames); | ||
return; | ||
} | ||
|
||
if (!funcName || funcName.length === 0) return; | ||
if (!projectName || projectName.length === 0) { | ||
localStorage.removeItem(funcName); | ||
}else if (projectName.length > 0) { | ||
localStorage[funcName] = projectName; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
// 設定された値を返す | ||
if (cmd === 'get-project-name') { | ||
var funcNames = request.func_names; | ||
var projectNames = {}; | ||
for (var i = 0; i < funcNames.length; i++) { | ||
var funcName = funcNames[i]; | ||
if (localStorage[funcName]) { | ||
projectNames[funcName] = localStorage[funcName]; | ||
} | ||
} | ||
sendResponse(projectNames); | ||
return; | ||
} | ||
// Clipboardに保持されたURLのページタイトルを返却する | ||
if (cmd === "get-clipboard-page") { | ||
const bg = browser.extension.getBackgroundPage(); | ||
/** @type {HTMLTextAreaElement | null} */ | ||
const textarea = document.querySelector("#daiiz-ctrlv"); | ||
if (!textarea) return; | ||
textarea.value = ""; | ||
textarea.focus(); | ||
bg.document.execCommand("paste"); | ||
resopondWebpageTitleOrRawText(textarea.value, sendResponse); | ||
} | ||
|
||
// Clipboardに保持されたURLのページタイトルを返却する | ||
if (cmd === 'get-clipboard-page') { | ||
const bg = window.app.extension.getBackgroundPage() | ||
const textarea = document.querySelector('#daiiz-ctrlv') | ||
textarea.value = '' | ||
textarea.focus() | ||
bg.document.execCommand('paste') | ||
resopondWebpageTitleOrRawText(textarea.value, sendResponse) | ||
} | ||
// URLのページタイトルを返却する | ||
if (cmd === "fetch-page-title") { | ||
const text = request.rawText; | ||
resopondWebpageTitleOrRawText(text, sendResponse); | ||
} | ||
}, | ||
); | ||
|
||
// URLのページタイトルを返却する | ||
if (cmd === 'fetch-page-title') { | ||
const text = request.rawText | ||
resopondWebpageTitleOrRawText(text, sendResponse) | ||
} | ||
}) | ||
|
||
const resopondWebpageTitleOrRawText = (text, sendResponse) => { | ||
if (text.match(/\n/)) return sendResponse(text) | ||
if (text.match(/^https?:\/\/scrapbox\.io\//)) return sendResponse(text) | ||
if (text.match(/gyazo\.com\//)) return sendResponse(text) | ||
if (text.match(/www\.youtube\.com\//)) return sendResponse(text) | ||
if (text.match(/www\.google/) && text.match(/\/maps\//)) return sendResponse(text) | ||
if (text.match(/^https?:\/\//)) { | ||
fetchPage(text) | ||
return | ||
} | ||
sendResponse(text) | ||
} | ||
const resopondWebpageTitleOrRawText = (/** @type {string} */ text, /** @type {(arg0: any) => void} */ sendResponse) => { | ||
if (text.match(/\n/)) return sendResponse(text); | ||
if (text.match(/^https?:\/\/scrapbox\.io\//)) return sendResponse(text); | ||
if (text.match(/gyazo\.com\//)) return sendResponse(text); | ||
if (text.match(/www\.youtube\.com\//)) return sendResponse(text); | ||
if (text.match(/www\.google/) && text.match(/\/maps\//)) return sendResponse(text); | ||
if (text.match(/^https?:\/\//)) { | ||
fetchPage(text); | ||
return; | ||
} | ||
sendResponse(text); | ||
}; | ||
|
||
const fetchPage = async (url) => { | ||
const res = await fetch(url, { | ||
credentials: 'include' | ||
}) | ||
const body = await res.text() | ||
const parser = new DOMParser() | ||
const doc = parser.parseFromString(body, 'text/html') | ||
console.log(doc.title) | ||
const fetchPage = async (/** @type {RequestInfo} */ url) => { | ||
const res = await fetch(url, { | ||
credentials: "include", | ||
}); | ||
const body = await res.text(); | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(body, "text/html"); | ||
console.log(doc.title); | ||
|
||
let externalLink = url | ||
const title = doc.title || null | ||
if (title) externalLink = `[${url} ${title}]` | ||
let externalLink = url; | ||
const title = doc.title || null; | ||
if (title) externalLink = `[${url} ${title}]`; | ||
|
||
console.log(externalLink) | ||
console.log(externalLink); | ||
|
||
if (isChrome()) { | ||
window.app.tabs.getSelected(null, tab => { | ||
window.app.tabs.sendMessage(tab.id, { | ||
command: 're:get-clipboard-page', | ||
externalLink | ||
}) | ||
}) | ||
} else { | ||
// Firefox extension | ||
const tab = await window.app.tabs.query({ | ||
currentWindow: true, | ||
active: true | ||
}) | ||
window.app.tabs.sendMessage(tab[0].id, { | ||
command: 're:get-clipboard-page', | ||
externalLink | ||
}) | ||
} | ||
} | ||
const tab = await browser.tabs.query({ | ||
currentWindow: true, | ||
active: true, | ||
}); | ||
browser.tabs.sendMessage(tab[0].id ?? 0, { | ||
command: "re:get-clipboard-page", | ||
externalLink, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
window.app = (/Chrome/.test(navigator.userAgent)) ? chrome : browser | ||
|
||
$(function () { | ||
window.daiizGyazo.manage.install() | ||
// @ts-ignore TSに移したときに型定義書く | ||
window.daiizGyazo.manage.install() | ||
// @ts-ignore TSに移したときに型定義書く | ||
.then(projectName => { | ||
window.daiizGyazo.textBubble.enable(projectName) | ||
}) | ||
}) | ||
// @ts-ignore TSに移したときに型定義書く | ||
window.daiizGyazo.textBubble.enable(projectName); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
document.querySelector('#btn-main-project').addEventListener('click', function () { | ||
var p = document.querySelector('#main-project').value; | ||
if (p && p.length > 0) localStorage['main-project'] = p; | ||
document.querySelector("#btn-main-project")?.addEventListener?.("click", () => { | ||
// @ts-ignore TSに移したときに型定義書く | ||
const p = document.querySelector("#main-project").value; | ||
if (p && p.length > 0) localStorage["main-project"] = p; | ||
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
window.app = (/Chrome/.test(navigator.userAgent)) ? chrome : browser | ||
|
||
$(function () { | ||
window.daiizScrapbox.manage.install() | ||
window.daiizScrapbox.iconButton.enable() | ||
window.daiizScrapbox.relCardsBubble.enable() | ||
window.daiizScrapbox.textBubble.enable() | ||
window.daiizScrapbox.pasteWebpageUrl.enable() | ||
}) | ||
// @ts-nocheck TSに移したときに型定義を書く | ||
window.daiizScrapbox.manage.install(); | ||
window.daiizScrapbox.iconButton.enable(); | ||
window.daiizScrapbox.relCardsBubble.enable(); | ||
window.daiizScrapbox.textBubble.enable(); | ||
window.daiizScrapbox.pasteWebpageUrl.enable(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
exports.isChrome = () => { | ||
export function isChrome() { | ||
return /Chrome/.test(navigator.userAgent); | ||
}; | ||
} | ||
|
||
exports.isFirefox = () => { | ||
export function isFirefox() { | ||
return /Firefox/.test(navigator.userAgent); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.