jquery - Make bootstrap modal work with another website using greasemonkey/tampermonkey (eg SO) -
i'm trying add couple of buttons, when clicked bootstrap dialogbox/modal should pop up. demo: http://jsbin.com/wiruhepere/1/edit?html,css,js,output
however when applying using greasemonkey/tampermonkey on real website, let's stackoverflow: it's not working @ !!
i'm suspecting script/css conflict maybe don't have knowledge track down :<
what i'm looking is:
- make modal appear when clicking on "delete" button
- when clicking on "ok" confirm, grab/intercept answer other stuffs...
please bear in mind i'm beginner in this, if soemthing isn't clear enough, feel free ask :-)
updated gm code based on woxxom comments:
// ==userscript== // @name bootstrap test // @namespace http://tampermonkey.net/ // @description why hell modal isnt working :< // @author enissay // @include http://stackoverflow.com/* // @include https://stackoverflow.com/* // @resource jqueryjs https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js // @resource bootstrapjs https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js // @resource buttoncss https://raw.githubusercontent.com/necolas/css3-github-buttons/master/gh-buttons.css // @resource bootstrapcss https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css // @resource githubbuttoniconset https://raw.githubusercontent.com/necolas/css3-github-buttons/master/gh-icons.png // @grant gm_addstyle // @grant gm_getresourcetext // @grant gm_getresourceurl // ==/userscript== (function() { //--- inject scripts & css including mycode/func $("head").append("<script>" + gm_getresourcetext("jqueryjs") + "</script>"); $("head").append("<script>" + gm_getresourcetext("bootstrapjs") + "</script>"); $("head").append("<style>" + gm_getresourcetext("bootstrapcss") + "</style>"); var githubbuttoniconset = gm_getresourceurl ("githubbuttoniconset"); var buttoncss = gm_getresourcetext("buttoncss"); buttoncss = buttoncss.replace (/gh-icons\.png/g, githubbuttoniconset); $("head").append("<style>" + gm_getresourcetext(buttoncss) + "</style>"); $("body").append("<script>("+myfunc+")();</script>"); })(); function myfunc () { (function() { 'use strict'; var deletebuttonhtml = ` <div class="button-group"> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mymodal"> launch demo modal </button> <a href="#" class="button icon edit">edit</a> <a href="#" class="button icon remove danger">delete</a> </div> `; var modalhtml = ` <!-- modal --> <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel">modal title</h4> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> <button type="button" class="btn btn-primary">save changes</button> </div> </div> </div> </div> `; //--- add nodes page $("body").prepend(deletebuttonhtml); $("body").prepend(modalhtml); //--- attach event button $(".button-group").find(".remove").click(function(evt){ //debugger; $('#mymodal').modal({ keyboard: true }); }); })(); }
initial gm code bellow:
// ==userscript== // @name bootstrap test // @namespace http://tampermonkey.net/ // @description why hell modal isnt working :< // @author enissay // @include http://stackoverflow.com/* // @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js // @require https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js // @resource buttoncss https://raw.githubusercontent.com/necolas/css3-github-buttons/master/gh-buttons.css // @resource bootstrapcss https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css // @resource githubbuttoniconset https://raw.githubusercontent.com/necolas/css3-github-buttons/master/gh-icons.png // @grant gm_addstyle // @grant gm_getresourcetext // @grant gm_getresourceurl // ==/userscript== (function() { 'use strict'; var deletebuttonhtml = ` <div class="button-group"> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mymodal"> launch demo modal </button> <a href="#" class="button icon edit">edit</a> <a href="#" class="button icon remove danger">delete</a> </div> `; var modalhtml = ` <!-- modal --> <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel">modal title</h4> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> <button type="button" class="btn btn-primary">save changes</button> </div> </div> </div> </div> `; //--- add nodes page $("body").prepend(deletebuttonhtml); $("body").prepend(modalhtml); //--- attach event button $(".button-group").find(".remove").click(function(evt){ $('#mymodal').modal({ keyboard: false }); }); //--- style our newly added elements using css. gm_addstyle (gm_getresourcetext ("bootstrapcss")); var githubbuttoniconset = gm_getresourceurl ("githubbuttoniconset"); var buttoncss = gm_getresourcetext("buttoncss"); buttoncss = buttoncss.replace (/gh-icons\.png/g, githubbuttoniconset); gm_addstyle(buttoncss); })();
there's many things wrong script, let me list them:
manually appending javascript dom - requiring other javascript resources, use
@require
such as:// @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
mistaking string , variable - bellow, you're using
getresourcetext
giving css string, not resource name:var buttoncss = gm_getresourcetext("buttoncss"); buttoncss = buttoncss.replace (/gh-icons\.png/g, githubbuttoniconset); // button css not contain name of resource, actual css $("head").append("<style>" + gm_getresourcetext(buttoncss) + "</style>");
unnecesary closures - doesn't cause errors, why wrap whole function body in self invoking closure? crazy:
// warning: crazy code, not use! function myfunc() { (function() { })() }
executing function appending html - while not error in general, people call
myfunction
:// call function myfunction();
not sure why did instead:
$("body").append("<script>("+myfunction+")();</script>");
this cannot work, because greasemonkey variable scope hidden global scope. impossible call
myfunction
html. means in case cannot work.
i fixed of problems you, still need figure out how resolve css conflicts dialog on stackoverflow, problem out of scope of answer question.
the script below works on http://blank.org:
// ==userscript== // @name bootstrap test // @namespace http://tampermonkey.net/ // @description why hell modal isnt working :< // @author enissay, tomas zato (http://stackoverflow.com/users/607407/tom%c3%a1%c5%a1-zato) // @include /https?:\/\/stackoverflow\.com\/*/ // @include http://blank.org/* // @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js // @require https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js // @resource buttoncss https://raw.githubusercontent.com/necolas/css3-github-buttons/master/gh-buttons.css // @resource bootstrapcss https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css // @resource githubbuttoniconset https://raw.githubusercontent.com/necolas/css3-github-buttons/master/gh-icons.png // @grant gm_addstyle // @grant gm_getresourcetext // @grant gm_getresourceurl // ==/userscript== console.log("start"); document.head.appendchild(csselement(gm_getresourceurl ("githubbuttoniconset"))); document.head.appendchild(csselement(gm_getresourceurl ("buttoncss"))); document.head.appendchild(csselement(gm_getresourceurl ("bootstrapcss"))); function csselement(url) { var link = document.createelement("link"); link.href = url; link.rel="stylesheet"; link.type="text/css"; return link; } function myfunc () { 'use strict'; var deletebuttonhtml = ` <div class="button-group"> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mymodal"> launch demo modal </button> <a href="#" class="button icon edit">edit</a> <a href="#" class="button icon remove danger">delete</a> </div> `; var modalhtml = ` <!-- modal --> <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel">modal title</h4> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> <button type="button" class="btn btn-primary">save changes</button> </div> </div> </div> </div> `; //--- add nodes page $("body").prepend(deletebuttonhtml); $("body").prepend(modalhtml); //--- attach event button // not necessary, bootsrap creates event listeners automatically /*$(".button-group").find("button").click(function(evt){ console.log("click.", $('#mymodal')); $('#mymodal').modal("show"); }); */ } myfunc();
Comments
Post a Comment