I created a custom Polymer element, and tried to use it in my Chrome extension. However, I ran into the following problem.
I can't access my element's API from the content script. Here is what I mean.
my-element's template
<link rel="import" href="../polymer/polymer.html">
<dom-module id="my-element">
<template>
... some html...
</template>
<script src="js/my-element.js"></script>
</dom-module>
in my-element.js
Polymer({
is: 'my-element',
init: function () {
console.log('init');
}
});
in content.js
var link = document.createElement('link');
link.setAttribute('rel', 'import');
link.setAttribute('href', chrome.extension.getURL('my-element.html'));
link.onload = function() {
var elem = document.createElement('my-element');
document.body.appendChild(elem);
elem.init(); // Error: undefined is not a function
};
document.body.appendChild(link);
What is interesting it's that I can see that my-element is rendered properly on page, with all it's shadow dom and things. And If I run document.querySelector('my-element').init() in the host webpage's console, it is executed without errors. However, executing the same code inside the content script yields an error.
I tried to include Polymer itself as a content script. I ran into registerElement problem, but I solved it thanks to this question. In this case, I have window.Polymer defined in my content.js. My-element's API becomes fully accessible, but now it's shadow dom is not rendered when I append it to the host webpage's DOM.
chrome-extension://links on a normal http/https page due to sandboxing, at least such links don't work here as is. Have you exposedmy-element.htmlvia web_accessible_resources in manifest? - wOxxOmmy-element.htmland all related css files in my manifest. The element is actually rendered and it's methods and properties declared withPolymer({...})are accessible withdocument.querySelector(), but only from the host webpage and not from content script. I think I should somehow imperatively declaremy-elementfrom within the content script, but I have no idea how. - optimistiks