Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Angular.js to new DOM Element in Chrome Extension Content Script

Okay, so I've been at this for a while.

In the red box, I want to have some angular functionality (ng-repeats, data binding, etc...) This red box appears when text on any webpage is double clicked. However, I can't seem to find out how to actually get angular wired/hooked up to the text box example in the red popup.

It seems fairly trivial to use angular in a badge popup in chrome, as well as on options pages, etc...but I can't seem to get it working in this instance.

example of a div element being added to the DOM of any webpage upon double-clicked text

inject.js (which is included as a content script in manifest, below)

    var displayPopup = function(event) {

    var mydiv = document.createElement('div'); 
    var $div = $('#divid').closest('.sentence');
    mydiv.innerHTML = getSelectionText();
    mydiv.innerHTML += currentSentence.innerHTML;

        //Next line is where I want to apply some angular functionality
        mydiv.innerHTML += '<div ng-app="myApp" scroll-to-me><input type="text" ng-model="data.test"><div ng-model="data.test">{{data.test}}</div></div>';



    mydiv.id = "popup";
    mydiv.style.position = "fixed";
    mydiv.style.top = event.clientY + "px";
    mydiv.style.left = event.clientX + "px";
    mydiv.style.border = "4px solid #d00";
    mydiv.style.background = "#fcc";

        $("body").append(mydiv);

    $.getJSON('http://local.wordly.com:3000/words/definitions/test', function(data) {
      console.log(data);
    });
  }

and my manifest.json content script array looks like:

"content_scripts": [
    {
      "matches": [
        "https://www.google.com/*"
      ],
      "css": [
        "src/inject/inject.css"
      ]
    },
    {
      "matches": [
        "http://*/*", 
        "https://*/*"
      ],
      "js": [
        "js/angular/angular.js", "app.js", "js/jquery/jquery.js", "src/inject/inject.js"
      ]
    }
  ]

and app.js, also included in manifest, for just some skeletal app to get up and running.

var myApp = angular.module("myApp", []);

myApp.factory('Data', function(){
    //return {message: "I'm data from a service"};
});

myApp.controller("SecondCtrl", function($scope, $http){

});
like image 694
freedomflyer Avatar asked May 06 '13 06:05

freedomflyer


People also ask

Why can’t I add angular-elements to my website?

If you need to add Angular-Elements to the site written in Angular — new impediments are popping up. If you run the Chrome Extension on a website written in Angular, you might see that Angular-Elements is not being loaded, and in the html markup there is an empty <custom-element />.

How to run @angular-elements in the extension context?

Angular-Elements must be run in the page context, otherwise the browser simply will not see yours registered Web Component. This is because in the extension context there is dedicated window object, which is not the same as the one from the loaded page. If you load the Angular-Elements script using the content_scripts in the manifest.

How to get the DOM content of an extension in chrome?

When you trying to get a DOM content in Chrome tab from an extension using document.getElementBy* or document.querySelector*, you get the DOM content of your extension HTML. Suppose that you have a following default popup (specified as page_action -> default_popup in manifest.json ).

How do content scripts access chrome APIs?

Content scripts can access Chrome APIs used by their parent extension by exchanging messages with the extension. They can also access the URL of an extension's file with chrome.runtime.getURL () and use the result the same as other URLs. Additionally, content scripts can access the following chrome APIs directly:


1 Answers

You need to bootstrap manually if you’re injecting the markup after the page loads. Angular will only run ng-app if it’s present when the document is loaded. Afterwards, you pass the module name to angular.bootstrap:

angular.bootstrap(mydiv, ['myApp'])

Example.

like image 54
Josh Lee Avatar answered Sep 28 '22 09:09

Josh Lee