Skip to content

Instantly share code, notes, and snippets.

@jmozmoz
Last active August 29, 2015 14:03
Show Gist options
  • Save jmozmoz/166982cf257771699ff4 to your computer and use it in GitHub Desktop.
Save jmozmoz/166982cf257771699ff4 to your computer and use it in GitHub Desktop.
Userscript to subscribe to threads at www.cfd-online.com/Forums/ with a single click
// ==UserScript==
// @name Add one-click subscription button
// @description Adds live example button, with styling.
// @include http://www.cfd-online.com/Forums/*.html
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant GM_addStyle
// @grant GM_openInTab
// @version 0.0.1
// ==/UserScript==
// Based on https://stackoverflow.com/questions/6480082/add-a-javascript-button-using-greasemonkey
// and https://stackoverflow.com/questions/5036737/how-do-you-make-greasemonkey-click-a-link-that-has-specified-text
/* If link to subscribe exists then show button */
var TargetLink = $("a:contains('Subscribe to this Thread')")
if (TargetLink.length) {
/*--- Create a button in a container div. It will be styled and
positioned with CSS.
*/
var zNode = document.createElement ('div');
zNode.innerHTML = '<button id="subscriptionButton" type="button">'
+ 'Subscribe to this thread</button>'
;
zNode.setAttribute ('id', 'myContainer');
document.body.appendChild (zNode);
//--- Activate the newly added button.
document.getElementById ("subscriptionButton").addEventListener (
"click", ButtonClickAction, false
);
}
document.addEventListener('keydown', function(e) {
// pressed alt+shift+ctrl+s
if (e.keyCode == 83 && e.shiftKey && e.ctrlKey && e.altKey && !e.metaKey) {
ButtonClickAction(null);
}
}, false);
function ButtonClickAction (zEvent) {
/*--- For our dummy action, we'll just add a line of text to the top
of the screen.
*/
var TargetLink = $("a:contains('Subscribe to this Thread')")
if (TargetLink.length)
GM_openInTab(TargetLink[0].href);
// window.location.href = TargetLink[0].href
}
//--- Style our newly added elements using CSS.
GM_addStyle ( multilineStr ( function () {/*!
#myContainer {
position: fixed;
top: 0;
left: 0;
font-size: 20px;
background: orange;
border: 3px outset black;
margin: 5px;
opacity: 0.9;
z-index: 222;
padding: 5px 20px;
}
#subscriptionButton {
cursor: pointer;
}
#myContainer p {
color: red;
background: white;
}
*/} ) );
function multilineStr (dummyFunc) {
var str = dummyFunc.toString ();
str = str.replace (/^[^\/]+\/\*!?/, '') // Strip function () { /*!
.replace (/\s*\*\/\s*\}\s*$/, '') // Strip */ }
.replace (/\/\/.+$/gm, '') // Double-slash comments wreck CSS. Strip them.
;
return str;
}
// ==UserScript==
// @name OneClickSubscription
// @namespace oneclicksubscription
// @description Subscribe to a thread on cfd-online.com with one click
// @include http://www.cfd-online.com/Forums/subscription.php?do=addsubscription&t=*
// @version 0.0.1
// @grant none
// ==/UserScript==
function main() {
//alert('forms.length: ' + document.forms.length);
for (var i = 0; i < document.forms.length; i++) {
var action = document.forms[i].action;
if (action.indexOf("doaddsubscription") > -1) {
window.console.log("found form!");
document.forms[i].submit();
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment