Skip to content

Instantly share code, notes, and snippets.

@lot224
Created January 18, 2019 18:37
Show Gist options
  • Save lot224/e4c6888e954e374db2668abe6b3dc8af to your computer and use it in GitHub Desktop.
Save lot224/e4c6888e954e374db2668abe6b3dc8af to your computer and use it in GitHub Desktop.
Inject ng app into running website via bookmarklet. (If security allows for it.)
/**
* Bookmarklet code, this will inject a script into what ever page your currently visiting when you click this favorite/bookmark link.
* To install, just create a favorite/bookmark, then view the properties of is and change the name to something familiar.
* Update the url to the next line, also you will need to modify the src attribute so that it points to the payload script you want to inject.
*/
javascript: (function () { var d = document; var h = d.getElementsByTagName('head')[0]; var s = d.createElement("script"); s.setAttribute("type", "text/javascript"); s.setAttribute("src", "http://www.somedomain.com/somescript.js"); h.appendChild(s); })();
var html = document.getElementsByTagName("html")[0];
var head = document.getElementsByTagName("head")[0];
var body = document.getElementsByTagName("body")[0];
var element;
var template = `
<div style="z-index:10000; color:#fff; background-color:rgba(0,0,0,.5); position: fixed; width:300px; top: 10px; right: auto; left: 10px; bottom: 10px; padding: 10px; border-radius: 10px;">
{{title}}
</div>
`
var fn = {
init: () => {
// If the site your injecting the script into doesn't already have angular, we'll add it.
// Note: This doesn't check versions, you may want to add that if your having conflicts with angular > 1.7
if (!window.angular)
fn.ng();
else
fn.onLoad();
},
ng: () => {
if (!window.angular) {
var ngScript = document.createElement('script');
ngScript.setAttribute("type", "text/javascript");
ngScript.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/angularjs/1.7.3/angular.js");
head.appendChild(ngScript);
// Wait a second for the stack to finish and for angular to get added before we continue.
setTimeout(() => {
// Make sure the page has a angular starting point.
head.setAttribute("ng-app", "application");
// Make sure there is a module present;
angular.module('application', []);
// Bootstrap angular into the app.
angular.element(function () {
angular.bootstrap(document, ['application']);
});
// Angular is ready to go
fn.onLoad();
}, 1000);
}
},
onLoad: () => {
// Create a new Element
element = document.createElement('injected-element');
// Add the template content into the element.
element.innerHTML = template;
//Inject the element as the first child of the body element
body.prepend(element);
// Execute the controller on the element.
angular.element(document).injector().invoke(fn.controller);
},
controller: ['$rootScope', '$compile', function ($rootScope, $compile) {
// Create a new isolated scope model to render with the view.
var scope = $rootScope.$new(true);
scope.title = "Hello World.";
// Apply the scope to the view.
$compile(element)(scope);
// Force a digest to update the view.
scope.$digest();
}]
}
// Start it off.
fn.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment