content script is not working when using gmail? [duplicate] - google-chrome-extension

I'd like to write a script that injects in the compose-page of gmail.
So far I've got this url-pattern: ["*://mail.google.com/*"]. The script successfully gets injected when loading gmail, but after that it doesn't get re-injected when clicking links like inbox or compose. I was hoping ["*://mail.google.com/*compose"] would do it easily, but no.
Is this pattern perhaps as far as I can get, and I'll then have to create a listener that checks when that part of the page is reloaded?
What is the most straightforward way to determine when the compose-page is loaded?

Content script only run when a page is truly loaded.
If you want to run code for a specific hash, inject the script at the URL without the hash, and use the hashchange to detect changes.
function checkHash() {
if (/^#(compose|drafts)\b/.test(location.hash)) {
// Do whatever you want
}
}
window.addEventListener('hashchange', checkHash);
checkHash();
Instead of monitoring the location hash, it might be more effective to use setInterval to continuously check whether the desired element exists in the DOM. This is particularly useful when you want to read or write the value of a specific DOM node.

Related

How to access client-side javascript variable from external page?

Let's say, when we open link https://example.com and that page generates javascript variable, called xyz, so we can access it from browser's Inspect console:
console.log(xyz);
However, how can we get & read that variable from node.js ? (the external link needs to be rendered like in browser, to get javascript values out of it).
There are different approaches to get the variable, depending how they are created.
variable is directly in source code so you can simply parse it using regEx for example.
variable is being evaluated in JS runtime, in this case you need to mock the browser environment using PhantomJS, which is quite heavy.
One possible way seems to use plugin like Nightwatch. However, in the past it depended selenium, which is a bit heavy.

How can I identify tab (or get its id) in Safari App Extension?

I am developing an Safari App Extension and I would like to identify opened tab. I found no id or no way to do that.
Chrome has an awesome API for doing that: https://developer.chrome.com/extensions/tabs. How can I do it with Safari App Extension (in Swift)?
Safari tab API is very poor and does not contain ID (https://developer.apple.com/documentation/safariservices/sfsafaritab)
You are right. The API is not easy to use. However, there is a way to get the ID. The SFSafariPage itself.
The SFSafariPage adopt NSSecureCoding. You can use savedPage.isEqual(newPage) to compare if the same page you get from getActiveTab or something. The page is a pointer or proxy. But the content is unique.
P.S
You should very careful about the SFSafariExtensionHandler. The handler is always a new object. Use the singleton model save page index could make sure data won't lost.
A little more detail would be helpful. What do you mean 'ID'? What do you actually want to do?
You can get access to tabs in your extension handler using SFSafariApplication API. I've not tested it myself, but it may look something like this:
SFSafariApplication.getActiveWindow { (window) in
window?.getActiveTab { (tab) in
...do something with the tab...
}
}
Could you please elaborate a bit what you are trying to achieve?
I suppose, at the worst case, you can inject a script in every page, where you will generate a random ID, send it to the host app, which will store them all into UserDefaults. Then later, when you need to retrieve a tab, you'd iterate over all tabs and establish some communication with the one you are targeting with dispatchMessageToScript.
SFSafariTab inherits hashValue from NSObject. This could allow you to identify your tab.

Correct way to override content being displayed?

I want to enable the use of codes inside of content on a Drupal website. For example, when creating a block or a node, i want users to be able to insert code like this:
[[EMAIL]]
Depending on what the current language is, it might display a different value. The tricky part is not retrieving the value I want to replace it with, but figuring out at what point I replace it?
What hook or function would I use, to replace any node content that has the specific code in it, with another value? And the same for a block or any other content that is going to be displayed?
I solved this by creating my own input filter. I copied this example.

Allow_url_include is there a way to allow includes from just one url

allow_url_include is there a way to allow includes from just one Url. maybe with Htaccess or in the PHP?
No. To fetch the contents of a URL, use file_get_contents(). Then, to send the result back to the waiting browser, just use echo().
$contents = file_get_contents('http://www.example.com/');
echo( $contents );
The above will require that you set config allow_url_fopen=1 (which is already set by default).
If you were to use include, instead of the approach I've shown above, there would be one major difference: include also executes any PHP code it finds inside the fetched document. In general, this is a really dangerous thing to allow unless you know that you control 100% of the contents of the document being included.
That said: if you do want code that works exactly like include, you can do something like the following (which is safer because it only fetches a single URL, and it doesn't require you to enable allow_url_include).
$contents = file_get_contents('http://www.example.com/');
eval('?>'.$contents);
If you choose do this, be certain that you control the full contents of the remote URL. If someone else controls that file, they will be able to execute arbitrary code on your server. Don't let that happen.
Well why you need that, you have control over the code, you can just use one url in while including. Beside using allow_url_include is very dangerous. If you still want to use, you can do it this way.
Make a custom function (A rough code)
function url_include($url){
//now check if $url is the one you want to be included
if($url=="http://www.mysite.com/file.php"){
include($url);
}else{
//show error or something.
}
}
now use this url_include("http://www.mysite.com/file.php"); in your page to include url.
Hope this helps

What is the best practice for checking each url visited in a chrome extension?

In a chrome extension I want to to check each url against a list of possible urls and then perform an action if one of the possible urls is visited. For example if Amazon.com was on the list then anytime a user went to Amazon.com the action would be performed.
There are around 5000 possible urls which are all in a mySQL database and can be outputted to a JSON file.
Should the extension check each url everytime a page is visited by checking the JSON file? Is there anyway of loading the JSON file into the extension? Is there a better way to do this?
You have (at least) two options, depending on the action you'd like to take when a particular URL is visited:
You can encode your filter in a JavaScript object that you load into the extension's background page at startup. The KMOO extension is a good example of how this might work: https://code.google.com/p/chrome-opt-out-extension/source/browse/#svn%2Ftrunk%2Fchrome
If you're injecting content scripts, you can restrict your extension directly in the manifest by adding appropriate filters. See the matches attribute at http://code.google.com/chrome/extensions/content_scripts.html for example.
The first option is probably what I'd recommend. Parsing a large JSON object into a lookup table once at startup isn't terribly expensive, and certainly better than reading it every time you'd like to make a decision.

Resources