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");}
})();
Related
// ==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.
I'm using CEF and have built the cefsimple.exe. I can include any html file to the simple_app.cpp which will start after the doubleclick. But how is it possible to start this cefsimple.exe in fullscreen mode? Which build do I need? I work with VS2013 on a win64 system.
SimpleApp::SimpleApp() {
}
void SimpleApp::OnContextInitialized() {
CEF_REQUIRE_UI_THREAD();
// Information used when creating the native window.
CefWindowInfo window_info;
#if defined(OS_WIN)
// On Windows we need to specify certain flags that will be passed to
// CreateWindowEx().
window_info.SetAsPopup(NULL, "cefsimple");
// my first try: *************************************
/* RECT winrect;
winrect.bottom = 0;
winrect.left = 0;
winrect.right = 0;
winrect.top = 0;
window_info.SetAsChild(NULL, winrect);*/
#endif
// SimpleHandler implements browser-level callbacks.
CefRefPtr<SimpleHandler> handler(new SimpleHandler());
// Specify CEF browser settings here.
CefBrowserSettings browser_settings;
std::string url;
// Check if a "--url=" value was provided via the command-line. If so, use
// that instead of the default URL.
CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine();
url = command_line->GetSwitchValue("url");
if (url.empty())
url = "file:///C:/Projekte/BOF-WENDT-HTML5/Fullscreen.html";
// Create the first browser window.
CefBrowserHost::CreateBrowser(window_info, handler.get(), url,
browser_settings, NULL);
}
Not yet implemented in CEF, see:
https://code.google.com/p/chromiumembedded/issues/detail?id=562
Update: Changes for this feature have landed https://bitbucket.org/chromiumembedded/cef/issue/562
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?
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.
Thanks to some awesome answers in other threads, I learned how to:
load jQuery and jQuery-UI in a bookmarklet
load a CSS into a bookmarklet using jQuery
create a DIV in a bookmarklet using jQuery
create jQuery UI dialog (external link)
and I could manage to combine all four things into a single script which works both as a BookMarklet and as a GreaseMonkey script, which is absolutely awesome.
However, it doesn't work on all sites. Neither the bookmarklet nor the GreaseMonkey script work on Wikipedia.
I tried it on other sites and it works: amazon.com, msn.com, yahoo.com, news.google.com, stackoverflow.com and it works well.
Is it possible to make it work on wikipedia.org?
Also there is a minor issue: each second time I use the bookmarklet, the title of the dialog window doesnt show ('Basic dialog'). I wonder why, although it doesn't really matter - it's not important at all.
Here is the script:
javascript:(function () {
function getScript(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0];
var completed = false;
script.onload = script.onreadystatechange = function () {
if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
completed = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
getScript("https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js", function () {
getScript("https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js", function () {
var myStylesLocation = "https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css";
$('<link rel="stylesheet" type="text/css" href="'+myStylesLocation+'" >').appendTo("head");
alert("It works");
var myMessage = "This is the default dialog which is useful for displaying information.";
$("<div id='dialog'; title='Basic dialog'; style='border:2px solid black; background-color:lightblue; font-size:80%'; <p>" + myMessage + "</p></div>").appendTo("body");
$( "#dialog" ).dialog();
});
});
})();