Last active
August 12, 2022 11:33
-
-
Save lotabout/a65b8a45e612d3feb083 to your computer and use it in GitHub Desktop.
Greasemonkey script for send content to backend localhost.
This file contains hidden or 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
// ==UserScript== | |
// @name Post test | |
// @namespace jinzhou | |
// @description Post Test | |
// @include * | |
// @version 1 | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js | |
// @grant GM_xmlhttpRequest | |
// @grant GM_addStyle | |
// ==/UserScript== | |
// CSS | |
GM_addStyle("#extractor-overlay { visibility: hidden; position: fixed; left: 0px; top: 0px; width:100%; height:100%; text-align:center; z-index: 1000; background: #333} #extractor-overlay > div { width:600px; margin: 100px auto; background-color: #fff; border:1px solid #000; padding:15px; text-align:center; } #extractor-content {width: 100%; min-height:350px}"); | |
function newSendDialog(id) { | |
var dialog = document.createElement('div'); | |
dialog.setAttribute('id', id); | |
dialog.innerHTML = '<div><p id="extractor-message"></p><textarea id="extractor-content"></textarea><div><button type="button" id="extractor-send">Send</button><button type="button" id="extractor-cancle">Cancle</button></div></div>'; | |
return dialog; | |
} | |
function toggleVisible(id) { | |
var el = document.getElementById(id); | |
el.style.visibility = (el.style.visibility === 'visible') ? 'hidden' : 'visible'; | |
} | |
function sendContent(content) { | |
GM_xmlhttpRequest({ | |
method: "POST", | |
url: "http://localhost:8000", | |
data: "content="+content, | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded" | |
} | |
}); | |
} | |
var dialog_id = 'extractor-overlay'; | |
var dialog = newSendDialog(dialog_id); | |
document.body.appendChild(dialog); | |
var cancleButton = document.getElementById('extractor-cancle'); | |
cancleButton.onclick = function() {toggleVisible(dialog_id);}; | |
// send button | |
var sendButton = document.getElementById('extractor-send'); | |
sendButton.onclick = function() { | |
var contentArea = document.getElementById('extractor-content'); | |
var extractorMsg = document.getElementById('extractor-message'); | |
if (contentArea.value !== null && contentArea.value !== "") { | |
sendContent(contentArea.value); | |
extractorMsg.innerHTML = "Send Done!"; | |
} else { | |
extractorMsg.innerHTML = "Content empty, not send!"; | |
} | |
toggleVisible(dialog_id); | |
}; | |
var toggleButton = newToggleButton(); | |
toggleButton.onclick = function(){ | |
setDefaultContent(); | |
toggleVisible(dialog_id); | |
}; | |
appendToggleButton(toggleButton); | |
// Now customize two parts! | |
// 1. insert the toggle button to where it belongs according to the url. | |
function newToggleButton() { | |
var url = window.location.href; | |
var ret = null; | |
if (url.match('www.bilibili.com/video/')) { | |
ret = document.createElement('a'); | |
ret.className = 'i-link ret'; | |
ret.innerHTML = '下载'; | |
} | |
return ret; | |
} | |
function appendToggleButton(btn) { | |
var url = window.location.href; | |
if (url.match('www.bilibili.com/video/')) { | |
var target = document.getElementById('i_menu_login_reg'); | |
target.appendChild(btn); | |
} | |
} | |
// 2. change the function for setting default content according to the url. | |
function setDefaultContent() { | |
var contentArea = document.getElementById('extractor-content'); | |
if (contentArea.value === null || contentArea.value === "") { | |
contentArea.value = window.location.href; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment