greasemonkey v3.8 #exclude does nothing - greasemonkey

// ==UserScript==
// #name Helloworld
// #namespace https://www.sample.com
// #exclude https://www.sample.com/php/*
// ==/UserScript==
alert("HELLO WORLD!");
Hello,
I have greasemonkey v3.8 and I want to exclude all "sample.com" url's containing "/php/*"
-How can I do this? For me the description above with "exclude" doesn't work.

Related

How do I write vim-latex like custom key bindings into the Overleaf (Codemirror) editor?

I am aware that Tampermonkey (https://www.tampermonkey.net/) could potentially do this. I installed a Tampermonkey chrome extension, and under "Dashboard", I added the following userscript (All the commented-out lines I tried below << CodeMirrorVim.Vim.map("jj", "", "insert");>> didn't work).
// ==UserScript==
// #name Overleaf Editor Custom VIM Keybindings
// #namespace http://tampermonkey.net/
// #version 0.1
// #match https://www.overleaf.com/project/*
// #grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener("UNSTABLE_editor:extensions", (event) => {
const { CodeMirror, CodeMirrorVim, extensions } = event.detail;
// add custom keybindings - insert mode applies on insert
CodeMirrorVim.Vim.map("jj", "<Esc>", "insert");
// CodeMirrorVim.Vim.map("kk", "Right-Right", "insert");
// CodeMirrorVim.Vim.map("ECI", "\\cite{<++>}<++>", "insert");
// CodeMirrorVim.Vim.map("aa", "<Down>", "insert");
// CodeMirrorVim.Vim.imap("`a", "\alpha", "insert");
// CodeMirrorVim.IMAP('FMB', "\\mathbb{<++>}<++>", 'insert')
// normal mode applies while escaped
// CodeMirrorVim.Vim.map("h", "j", "normal");
// CodeMirrorVim.Vim.map("hh", "<Right>", "normal");
});
})();
CodeMirrorVim.Vim.map("jj", "<Esc>", "insert");
works but
CodeMirrorVim.Vim.map("kk", "<Right>", "insert");
CodeMirrorVim.Vim.map("kk", "Right-Right", "insert");
CodeMirrorVim.Vim.map("kk", "<Esc>-<Right>", "insert");
all didn't work. So how do I write, for example, the following bindings in 'tex.vim" in Tampermonkey userscript?
Here is keybindings in my "tex.vim":
call IMAP ('FTX', "\\text{<++>}<++>", 'tex')
call IMAP ('FCA', "\\mathcal{<++>}<++>", 'tex')
call IMAP ('EBM', "\\begin{bmatrix}\<CR><++>\<CR>\\end{bmatrix}", 'tex')
imap kk <C-j>
Also it would be nice if we could input greeks in overleaf CodeMirror editor as we do using vim-latex:
typing "`a" gives "\alpha".

EsLint - Suppress "Do not use 'new' for side effects"

I saw the way to suppress this with jsLint, tried it, it did not work.
I need the 'new' keyword or my script does notwork.
How can I suppress it in .eslintrc?
Many Thanks
Update: Per Jordan's request.
[Please note my app is written in ReactJs]
// 3rd party
const AnimateSlideShow = require('../js-animation');
export default class Animate extends React.Component {
.......
fetchJsAnimation() {
const animation = this.refs.Animation;
new AnimateSlideShow(animation);
}
......
}
Error: Do not use 'new' for side effects no-new
Now, if I satisfy EsLint, my app craps out:
Uncaught (in promise) TypeError: Cannot set property '_handleMouse' of undefined(…)
Here's the documentation for the ESLint rule in question: http://eslint.org/docs/rules/no-new.html
Disallow new For Side Effects (no-new)
The goal of using new with a constructor is typically to create an object of a particular type and store that object in a variable, such as:
var person = new Person();
It's less common to use new and not store the result, such as:
new Person();
In this case, the created object is thrown away because its reference isn't stored anywhere, and in many cases, this means that the constructor should be replaced with a function that doesn't require new to be used.
I pasted that above because I think it's important to understand what the intent of the rule is, and not just how to make it go away.
If you can't find a way to get rid of new, you can suppress this error with the eslint-disable directive:
fetchJsAnimation() {
/* eslint-disable no-new */
const animation = this.refs.Animation;
new AnimateSlideShow(animation);
}
ESLint directives are block-scoped, so it will be suppressed inside this function only. You can also suppress rules on a single line with the eslint-disable-line directive:
new AnimateSlideShow(animation); // eslint-disable-line no-new
// You can disable the check on the next line as well.
// eslint-disable-next-line no-new
new AnimateSlideShow(animation);
If you really need to disable this rule for your entire project, then in your .eslintrc's "rules" section set the value for this rule to 0:
{
// ...
"rules": {
"no-new": 0,
// ...
}
}
You can also make it a warning instead of an error by setting it to 1 (2 is error).
Try to cover your function into an anonim function
(()=>code)();
in your example
fetchJsAnimation() {
const animation = this.refs.Animation;
(()=>new AnimateSlideShow(animation))();
}
Or you can use this pattern for example modern javascript framework eg. vuejs vue
Here is an example
(() => new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
}))();
Extending on sarkiroka answer, here's an ES5 version (essentially an IIFE with a return statement):
(function (Vue) {
'use strict';
return new Vue({
el: '.unity-header-wrapper'
});
}(Vue));
We're avoiding ESLint unused var error, which appears if used this way:
var myApp = new Vue({
el: '.unity-header-wrapper'
});
We're also avoiding using standalone 'new Vue()' instantiation (which prevents side effects error on ESLint)
var myApp = new Vue({
el: '.unity-header-wrapper'
});
You can also add Vue as a global in ESLint config, to avoid undefined global var error, as seen here: Global variables in Javascript and ESLint
// .eslintrc.json
"globals": {
"Vue": true
}
I use a more declarative form using an init() method inside my class. For example:
class Example {
constructor () { ... }
init () { //this method is using for initialize the instance }
}
So, when you initialize that instance:
const example = new Example()
example.init()
And with this you can avoid the "no new" linter and avoid undefined global without linter comments.

Greasemonkey: click a link

I would like to click on this link using Greasemonkey:
<a class="bbbx-button bbbx-button-grey" id="vote-button" data-bbbx-trigger="module.vote.submit" data-bbbx-id="51526" data-bbbx-score="10" data-bbbx-vote-type="up"><img src="//du3rc6beq6sv9.cloudfront.net/release1406643004647-5-1-2-1/assets/skins/default/images/vote/heart.png"> vote! <small id="vote-count">( 14 )</small></a>
I tried this code:
// ==UserScript==
// #name test
// #namespace test
// #include *
// #version 1
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// #grant GM_addStyle
// ==/UserScript==
console.log("Start");
window.setTimeout(start, 7000);
function start()
{
$("a#vote-button")[0].click();
console.log("End clicktime");
}
But unfortunately I can't find the problem.
You don't have jQuery loaded in your GM script.
Add this:
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
Feel free to change the version.
Have a look at this question, it also provides some solutions for websites that already have jQuery loaded: How can I use jQuery in Greasemonkey?

GreaseMonkey is not processing my button click

I want to get GreaseMonkey to process a button click. The HTML for the button is generated in a perl CGI which is accessed by GM_xmlhttprequest. The javascript to handle the click is in my user script.
Here is the user script. It prepends a div to the top of a webpage and populates that div with what comes from my CGI via AJAX.
// ==UserScript==
// #name button test
// #namespace http://www.webmonkey.com
// #description test that I can intercept a button and process click with AJAX
// #include http*
// #version 1
// #grant GM_xmlhttpRequest
// ==/UserScript==
function processButton() {
alert("got to processButton");
}
var myDiv;
var details = GM_xmlhttpRequest({
method: 'GET',
url:"http://localhost/cgi-bin/buttonTest",
onload: function (response) {
myDiv = document.createElement('div');
myDiv.id = 'mydiv';
myDiv.style.border = '4px solid #000';
myDiv.innerHTML = response.responseText;
document.body.insertBefore(myDiv, document.body.firstChild);
}
});
Here's my CGI.
#!/usr/bin/perl -w
print "Content-type:text/html\n\n";
print qq|<button onclick="processButton()">Click here</button>| ;
When I load a web page I get a new div with the HTML button code in it as I expect. When I click the button nothing happens. No alert. I created an HTML example to make sure I wasn't doing something really stupid. The example works fine.
<html>
<head>
<script>
function processButton() {
alert("got to processButton");
}
</script>
</head>
<body>
<button onclick="processButton()">Click here</button>
</body>
</html>
There's a console error message:
ReferenceError: processButton is not defined
What am I missing?
If I declare the processButton function in the HTML code that includes the button declaration and then put this in my userScript then my code works:
unsafeWindow.processButton = function() {
alert("hijacked processButton");
}
In other words, I'm now able to hijack the button from GM.

Create a "config" or "options" page for a Greasemonkey script

I've written a simple Greasemonkey script, and I'm trying to create a "config" page for this script (like the one that is used for Google Chrome extensions. ) Would there be any way to create a config page for a userscript, like the "options" pages for Google Chrome extensions? There isn't any way to include an .html page as part of a Greasemonkey script (as far as I know), so I'm looking for other options.
// ==UserScript==
// #name Redirector
// #namespace http://use.i.E.your.homepage/
// #version 0.1
// #description enter something useful
// #match http://*/*
// #copyright 2012+, You
// #run-at document-start
// ==/UserScript==
redirectToPage("http://www.youtube.com/", "http://www.google.com");
function redirectToPage(page1, page2){
if(window.location.href.indexOf(page1) != -1){
window.location.href = page2;
}
}
There are a few libraries which provide config pages for userscripts:
1) GM_config
2) MonkeyConfig
3) GM_registerMenuCommand Submenu JS Module
The usage varies per library, but typically you grant the permissions they need such as GM_getValue and GM_setValue, and require the library via the #require directive, e.g.:
// ==UserScript==
// #name My Userscript
// #description An example userscript with a config page
// #version 0.0.1
// #require https://www.example.com/lib/myconfig.js
// #grant GM_getValue
// #grant GM_setValue
// #grant GM_addStyle
// #grant GM_registerMenuCommand
// ==/UserScript==
const config = new MyConfig({ ... })
You then register a menu command which opens the config page/dialog, e.g.:
GM_registerMenuCommand('Configure My Userscript!', () => {
config.open()
})
In the case of MonkeyConfig, it can register the command for you:
const config = new MonkeyConfig({
title: 'Configure My Userscript!',
menuCommand: true,
// ...
})
For advanced uses, the configurator may allow listeners to be registered for the close/cancel/save events, as well as providing control over the CSS, and other options. Detailed instructions can be found on the GM_config wiki and the MonkeyConfig homepage.
If you are using it for chrome, then it isn't Greasemonkey but Tampermonkey.
You may consider using GM_getResourceText, paste your html to pastebin.com (or similar) and add the link as one of #resource to the metadata block.
At least, I know it works to Greasemonkey.
For example:
// #resource configHtml http://pastebin.com/raw.php?i=2RjKfwJQ
// ... some DOM node that you will append to the current page
node.innerHTML = GM_getResourceText("configHtml");
This is much needed but for now combination of 2 approaches should work.
1) For personal use I just have a bunch of variables at the top of the script. The problem here is that if anyone else uses my script an update ends up erasing his preferences.
2) Have a configuration page on your website. While this works wonderfully websites get deleted all the time. There is no good reason for a script to depend on a website to work.
If you do both those things the user can edit the preferences in the script when the scripts website vanishes.
Here is an example where undesired functionality is // commented out.
http://go-here.nl/gm/wikipedia-clean-up.php
Good luck and enjoy
Use a parameter to the page you're already including, and if that's set then clear the whole document:
http://page.my.script.runs.on/?configPage=true
if(getUrlParameter("configPage") === "true") {
$(document).empty
}
this userscript don't use any external libs. with modification you can make good UI.
// ==UserScript==
// #name UI development
// #namespace http://tampermonkey.net/
// #version 0.1
// #description try to take over the world!
// #author ManojBhakarPCM
// #match https://www.google.com/
// #grant none
// ==/UserScript==
(function() {
'use strict';
function ui_create(contentss){
var div = document.createElement('div');
//var contentss = "<h1>TESTsssss</h1>";
div.innerHTML = '<div id="mbpcm_modal" style="position: fixed;z-index: 1;left: 0;top: 0;width: 100%;height: 100%;overflow: auto;display:none;"><div id="mbpcm_content" style="background-color: pink;margin: 15% auto;padding: 20px;border: 1px solid black;width: 20%;box-shadow:20px 20px 30px 2px gray;border-radius:7px;"><span id="mbpcm_close" style="float: right;font-size:25px;cursor: pointer;">×</span>'+ contentss +'</div></div>';
document.body.appendChild(div);
var modal = document.getElementById("mbpcm_modal");
var close = document.getElementById("mbpcm_close");
close.onclick = function(){modal.style.display = "none";}
}
function ui_add_opener(parent,classs,stylee){
var btn = document.createElement("button");
btn.setAttribute("class",classs);
btn.setAttribute("style",stylee);
btn.setAttribute("id","mbpcm_ui_opener");
btn.innerHTML = "Settings";
parent.appendChild(btn);
var modal = document.getElementById("mbpcm_modal");
var btnn = document.getElementById("mbpcm_ui_opener");
btnn.onclick = function(){modal.style.display = "block";}
}
function ui_addElement(el,cls,styl,val,innerHtml){
var modal = document.getElementById("mbpcm_content");
var e = document.createElement(el);
e.setAttribute("class",cls);
e.setAttribute("style",styl);
e.setAttribute("value",val);
e.innerHTML = innerHtml;
modal.appendChild(e);
return e;
}
ui_create("<h1>Title</h1><span>discription</span><br>");
ui_add_opener(document.getElementById("gbw"),"RNmpXc","nothing");
ui_addElement("button","modal","none","none","TestButton").onclick = function(){alert("hellow ji ");}
ui_addElement("br","","","","");
ui_addElement("button","none","none","none","TestButton").onclick = function(){alert("How are you");}
ui_addElement("button","none","none","none","TestButton").onclick = function(){alert("Kaise ho");}
})();

Resources