I'm making an extension for chrome to visualize the js structure but for some reason I am not able to access custom js variables from the extension. How could I read these vars/properties? like knowing if jQuery exist?
Content scripts run in isolated world:
They cannot:
Use variables or functions defined by web pages or by other content scripts
You should try injecting a <script src='injected.js'><script> (this script should be able to 'see' the web page defined variables and functions) to page header from content script and then establishing a communication between these two. Content script and injected script can talk simply over DOM (by ie. posting messages in a hidden div) or, better, using postMessage/addEventListener.
Related
So in my manifest I'm referencing a copy of jQuery along with my content script.
Since this gets appended to the end of whatever page matches my url scheme (in this case every http/https page), would this impact pages that are already referencing some version of jQuery?
No, it wouldn't affect the host pages' scripts.
According to documentation:
Content scripts live in an isolated world, allowing a content script
to makes changes to its JavaScript environment without conflicting
with the page or additional content scripts
I'm developing a Chrome packaged app which displays a certain kind of document as HTML. I have the app working to some degree, but would like to add a feature allowing the user to open a file by clicking on a link to an applicable file.
I am able to launch the app by MIME type as per the docs here, and am familiar with the pp::Instance::HandleDocumentLoad method to handle the clicked link's source, but am unsure how to display HTML I'm generating from the parsed document.
This is easy enough to do when the user manually launches the app and selects a file using an input element and the HTML file system since the HTML GUI is specified in the app manifest, but as far as I can tell, launching based on MIME type just embeds the NMF.
TL;DR: Is there a way to specify a HTML interface for (or a simple way to render HTML from) a NaCl module instance created by a nacl_modules manifest entry?
This is possible, but it's a bit of a hack. I copied the trick from here:
https://groups.google.com/d/msg/native-client-discuss/UJu7VXvV_bw/pLc19D50gbwJ
You can see how I did it here and here:
Basically, you listen on chrome.tabs.onCreated and chrome.tabs.onUpdated, then you inject a small bit of JavaScript that checks for the embed element with the correct mimetype. If it finds the element, it sends a message (via chrome.runtime.sendMessage) to your extension. When your extension gets that message, it injects the rest of your JavaScript into the page using chrome.tabs.executeScript. At this point you can display whatever you want.
You could do it earlier, by injecting your code into every page, but I found this was a bit nicer, as it only injects a small bit of code.
I'm trying to access a global var (timer) on a webpage using the content.js script via my google chrome extension.
However, everytime it returns undefined, even though I can easily access it via the developer console.
var mySocket;
console.log('content.js');
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.task){
case "socketInjection":
window.setTimeout(
function(){ console.log(timer);}, 5000);
break;
}
});
Im using a setTimeout routine there, to make sure the page has loaded completely (which it should've anyways).
Anyone has a solution?
Thanks in advance, Daniel
The extension and the content script both have a different global scope than the page, so if there's something like timer = 5 in the extension or on the page, that isn't visible in the content script.
See
https://developer.chrome.com/extensions/content_scripts
However, content scripts have some limitations. They cannot:
Use variables or functions defined by their extension's pages
Use variables or functions defined by web pages or by other content scripts
This answer discusses some options:
Share in-memory objects in Chrome extension content scripts?
I missunderstood the context of content.js
Content scripts execute in a special environment called an isolated
world. They have access to the DOM of the page they are injected into,
but not to any JavaScript variables or functions created by the page.
It looks to each content script as if there is no other JavaScript
executing on the page it is running on. The same is true in reverse:
JavaScript running on the page cannot call any functions or access any
variables defined by content scripts.
Isolated worlds allow each content script to make changes to its
JavaScript environment without worrying about conflicting with the
page or with other content scripts. For example, a content script
could include JQuery v1 and the page could include JQuery v2, and they
wouldn't conflict with each other.
Another important benefit of isolated worlds is that they completely
separate the JavaScript on the page from the JavaScript in extensions.
This allows us to offer extra functionality to content scripts that
should not be accessible from web pages without worrying about web
pages accessing it.
I want to modify chrome start page, change background and maybe logo.
I want to include jQuery in users local storage and load it while users page loads ( search page )
I never done anything with chrome so I want some way where to start. Do I go reading with apps or extensions?
As a rule of thumb: Choose an app if you want to develop an (independent) application that can stand on its own. If you want to interact with the browser (e.g. modify a web page), build an extension.
In your case, you definitely need an extension.
Change start page - use chrome_url_overrides in the manifest file to override newtab.
Locally store jQuery: Although it's possible to store jQuery in local storage, you're probably fine with packaging jQuery with your extension.
To add it to your user's "search page", you have to use a content script. By default, content scripts run in an execution environment that is different from the page (the document's DOM is shared though). Usually, this behavior is desired. If you really want to expose the jQuery library to the scripts in the page, take a look at this answer.
If you really want to load some script from a remote location and use it as a content script, read Chrome extension adding external javascript to current page's html.
I'm not sure what you mean by "change background and maybe logo". If you're referring to the browser's appearance, the only option to do that is by creating a theme. This must be a separate extension.
As you may know, js files on chrome extensions and pages cannot directly access each other and they run on isolated worlds.
However, I want to access some of the functions on a page and call those functions from the plugin.
And I do not want to make my own version of those functions.
I'm wondering if this is possible... Would appreciate all the answers.
EDIT: The functions are on the background page. Its a browser action extension.
more info: Basically, I have a context menu which creates a tab to submit url to a page. however, re-opening the page makes many tabs and opening so many pages takes time. So I am willing to call the javascript function on the page directly from the extension if one instance of the page is open already. And I have access to the page permission-wise.
TL;DR: Need a way to call a javascript function on a page (without copying it locally) from an extension.
Assuming you know the tab ID of the page whose function you want to call, you can use chrome.tabs.update(tabId, {url: 'javascript:functionNameHere()'}); from your extension page. This works because javascript: URLs are mapped to script execution in the main (i.e. page, not isolated) world.