Where is the uri of the new window, in create-web-view? - html

According to the documentation, the create-new-window signal is called when a webkit is creating a new window. I've been trying to override this to handle <a target='_blank' links in PyGTK webkit browser. In a subclass of WebView I have:
...
self.connect("create-web-view", self.newWin)
...
def newWin(view, frame, data):
print view.get_property('uri')
print frame.get_property('uri')
print data.get_property('uri')
It is called when a new-window link is clicked, but for some reason all of these objects show the same url, the terminal prints out the current page url three times. How can I find the url that is supposed to be passed to a new window?
Thanks to ptomato, I found a solution. Setting the signal to this function works:
...
self.connect("new-window-policy-decision-requested", self.newWin) #requires webkit 1.1.4
...
def newWin(self, view, frame, request, nav_action, policy_decision):
"""
Calls the default browser on external link requests.
"""
functiontoviewurl(request.get_uri())
# According to the documentation: http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html#WebKitWebView-new-window-policy-decision-requested
# call ignore on the policy decision, then return true (that is, we handled it).
policy_decision.ignore()
return True

You can't intercept the creation of a new window by catching that signal - by that time, the browser has already decided it will create a new window. Instead, connect to new-window-policy-decision-requested and get the URI from the request parameter.

Related

Unable to watch loading state of a dbc.Container

My goal in my multi-page app is to make the page load itself a trigger for a specific page’s callback, but since I don’t think this is possible, I am attempting to watch the loading state of the page’s dbc.Container.
When I run something similar to below, per the docs, I would have expected it to print a dict with the component name and True for the loading state. Instead, it prints None.
Can anyone offer any input on how to achieve this goal? thank you
#dash.callback(
Output("update_message", "children"),
Input('main_container', 'loading_state'),
)
def func(loading_state):
print(loading_state)
def layout():
return dbc.Container([***page contents here], id='main_container', fluid=True)

Problem dropping same file twice in a row

I'm using the Dash Upload component, which in turn uses react-dropzone.
I can drag a file into the component and the corresponding callback will fire.
I can then drag a different file into the component and the callback will fire again.
But, if I drag a file into the component (which fires the callback) and then drag the same file into the component again, the callback does not fire.
There's a demo app in this Gist that demonstrates the behavior.
Searching for similar problems (stack-overflow, github) suggests that this behavior is to be expected because from the browser's point of view nothing has changed. Both of those discussions seem to end up with solutions that involve setting the .value part of some element to '', so that the browser sees the second drop event as a change.
Chriddyp contributed links to the relevant bit of code in Dash and pointed me to the react-dropzone component.
Is there a way to make dropping the file twice in a row work in Dash using react-dropzone?
Thanks!
g.
In Dash, callbacks are invoked every time a property changes. If you upload the same file a second time, the properties (e.g. the file name) are unchanged, and the callback will thus not be invoked. This is expected behavior.
To ensure that a callback is invoked every time, you must ensure that the Input property actually changes. One option would be to add a new property to the Upload component similar to the n_clicks property of buttons, say n_uploads, which is incremented each time a file is uploaded.
The easiest solution for the problem at hand would probably be to use the custom dash uploader instead. Among other things, it supports uploading the same file multiple times.
A bit late answer. I found a solution which is just reset the contents and filename to None in the Output of the callback.
A simple example
import dash
from dash import dcc, html
from dash.dependencies import Input, Output, State
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(html.Button('Upload File', id='btn_id'), id='upload_id'),
html.P(id='show_id'),
])
#app.callback(
Output('show_id', 'children'),
Output('upload_id', 'contents'),
Output('upload_id', 'filename'),
Input('upload_id', 'contents'),
State('upload_id', 'filename'),
State('btn_id', 'n_clicks'),
)
def uploaded_a_file(contents, filename, n_clicks):
if not contents:
raise dash.exceptions.PreventUpdate
msg = f'Uploaded {filename} for {n_clicks} time.'
return msg, None, None
if __name__ == '__main__':
app.run_server(debug=True)
Another workaround, similar to #aura's, is to replace the upload component entirely with a callback. This strategy can be useful when replacing "contents" would lead to circular callbacks.
See https://github.com/plotly/dash-core-components/issues/816#issuecomment-1032635061

Programmatic injection on nested iframes in extension page

Summary: I need to find a way to accomplish with programmatic injection the same exact behaviour as using content_scripts > matches with "all_frames": true on a manifest. Why? because it is the only way I've found of injecting iframe's content in an extension page without having Cross-Origin errors.
I'm moving to optional_permissions on a Chrome extension and I'm on a dead end.
What I want:
Move this behaviour to optional_permissions in order to be able to add more hosts in the future. With the current code, by adding one new host on content_scripts > matches the extension is disabled by Chrome.
For the move, I removed content_scripts in the manifest and I added "optional_permissions": ["*://*/"],. Then, I successfully implemented a dialog asking new permissions to the user with chrome.permissions.request.
As I said before, the problem is how to inject the iframe's content in an extension page.
What I've tried:
chrome.declarativeContent.RequestContentScript (mentioned here) with allFrames: true. I can only see the script running if I enter the URL directly, nothing happens when that URL is set in an iframe.
chrome.tabs.onUpdated: url is undefined for an extension page. Also, the iframe url is not detected.
Call chrome.tabs.executeScript with allFrames: true as soon as I load the first iframe. By doing this I get an exception Cannot access contents of the page. Extension manifest must request permission to access the respective host. and the "respective host" is chrome-extension://, which is not a valid host if you want to add it to the permissions.
I'm lost. I couldn't find a way to simulate the same behaviour as content_scripts > matches with programmatic injection.
Note: using webNavigation API is not an option since the extension is live and it has thousands of users. Because of this, I can not use the frameId property for executeScript. Thus, my only option with executeScript was to inject all frames but the chrome-extension host issue do not let me continue.
Update: I was able to accomplish what I wanted but only on an HTTP host. I used chrome.tabs.executeScript (option 3).
The question remains on how to make this work on an extension page.
You cannot run content scripts in any extension page, including your own.
If you want to run code in a subframe of your extension page, then you have to use frameId. There are two ways to do this, with and without webNavigation.
I've put all code snippets in this answer together (with some buttons to invoke the individual code snippets) and shared it at https://robwu.nl/s/optional_permissions-script-subframe.zip
To try it out, download and extract the zip file, load the extension at chrome://extensions and click on the extension button to open the test page.
Request optional permissions
Since the goal is to programmatically run scripts with optional permissions, you need to request the permission. My example will use example.com.
If you want to use the webNavigation API too, include its permission in the permission request too.
chrome.permissions.request({
// permissions: ['webNavigation'], // uncomment if you want this.
origins: ['*://*.example.com/*'],
}, function(granted) {
alert('Permission was ' + (granted ? '' : 'not ') + 'granted!');
});
Inject script in subframe
Once you have a tab ID and frameId, injecting scripts in a specific frame is easy. Because of the tabId requirement, this method can only work for frames in tabs, not for frames in your browserAction/pageAction popup or background page!
To demonstrate that code execution succeeds, my examples below will call the next injectInFrame function once the tabId and frameId is known.
function injectInFrame(tabId, frameId) {
chrome.tabs.executeScript(tabId, {
frameId,
code: 'document.body.textContent = "The document content replaced with content at " + new Date().toLocaleString();',
});
}
If you want to run code not just in the specific frame, but all sub frames of that frame, just add allFrames: true to the chrome.tabs.executeScript call.
Option 1: Use webNavigation to find frameId
Use chrome.tabs.getCurrent to find the ID of the tab where the script runs (or chrome.tabs.query with {active:true,currentWindow:true} if you want to know the current tabId from another script (e.g. background script).
After that, use chrome.webNavigation.getAllFrames to query all frames in the tab. The primary way of identifying a frame is by the URL of the page, so you have a problem if the framed page redirects elsewhere, or if there are multiple frames with the same URL. Here is an example:
// Assuming that you already have a frame in your document,
// i.e. <iframe src="https://example.com"></iframe>
chrome.tabs.getCurrent(function(tab) {
chrome.webNavigation.getAllFrames({
tabId: tab.id,
}, function(frames) {
for (var frame of frames) {
if (frame.url === 'https://example.com/') {
injectInFrame(tab.id, frame.frameId);
break;
}
}
});
});
Option 2: Use helper page in the frame to find frameId
The option with webNavigation looks simple but has two main disadvantages:
It requires the webNavigation permission (causing the "Read your browsing history" permission warning)
The identification of the frame can fail if there are multiple frames with the same URL.
An alternative is to first open an extension page that sends an extension message, and find the frameId (and tab ID) in the metadata that is made available in the second parameter of the chrome.runtime.onMessage listener. This code is more complicated than the other option, but it is more reliable and does not require any additional permissions.
framehelper.html
<script src="framehelper.js"></script>
framehelper.js
var parentOrigin = location.ancestorOrigins[location.ancestorOrigins.length - 1];
if (parentOrigin === location.origin) {
// Only send a message if the frame was opened by ourselves.
chrome.runtime.sendMessage(location.hash.slice(1));
}
Code to be run in your extension page:
chrome.runtime.onMessage.addListener(frameMessageListener);
var randomMessage = 'Random message: ' + Math.random();
var f = document.createElement('iframe');
f.src = chrome.runtime.getURL('framehelper.html') + '#' + randomMessage;
document.body.appendChild(f);
function frameMessageListener(msg, sender) {
if (msg !== randomMessage) return;
var tabId = sender.tab.id;
var frameId = sender.frameId;
chrome.runtime.onMessage.removeListener(frameMessageListener);
// Note: This will cause the script to be run on the first load.
// If the frame redirects elsewhere, then the injection can seemingly fail.
f.addEventListener('load', function onload() {
f.removeEventListener('load', onload);
injectInFrame(tabId, frameId);
});
f.src = 'https://example.com';
}

How do you pass options to a WinJS Settings page

Passing options to a regular PageControl via options is fine, but how do you pass options to a Settings page?
i.e. Settings pages using a SettingsFlyout still have the "ready: function (element, options)" event, but how do you set the options? The navigation is set in the application activated event, but there doesn't seem to be the opportunity to set options...
WinJS.Application.onsettings = function ( e )
{
e.detail.applicationcommands =
{
"about": { href: "/settings/about/about.html", title: "About" },
};
WinJS.UI.SettingsFlyout.populateSettings( e );
}
Like #devhammer said, you're not navigating to the settings page. In fact, it's a SettingsFlyout that is just showing up over your current page. There's not really any need to pass anything though because the SettingsFlyout will have access to the state of your page. You can define a variable in your main page and then access it on the settings page.
If you have application level data, you can just tack some state onto the app object. What I do is move the var app = WinJS.Application line that is in the default.js by default into global scope so I can access it from anywhere, and then I use that from where I need.
By the way, don't listen to the people that tell you that global scope is evil. As long as you're aware of what you're putting in global scope and why, it's just fine to use.
Given that you're not navigating to the page using the normal WinJS.Navigation.navigate method, I'm not sure you can pass any state beyond the title and URI for the settings pages you wish to add.
As such, the options argument on the settings page will always be undefined.

Actionscript works when tested in flash, but not on html page?

I am trying to create an ad for a website. When someone clicks on the ad, it is supposed to redirect them to a website, and register the click with google analytics.
I have done this with the following script:
import flash.external.ExternalInterface;
movieClip_3.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void {
trace("hi");
ExternalInterface.call("console.log", "test");
//ExternalInterface.call("_gaq._trackPageview", "/vpv/annoncer/[firmanavn.dk]");
navigateToURL(new URLRequest("http://www.google.com"), "_blank");
}
When i run this using preview->flash and i click on the surface, (where there is a big red square called movieClip_3) It opens the webpage. However when i try to publish as html, the big red square shows, but nothing happens on click. Not even console.log. I have tried setting allowscriptaccess = always but that does not change anything.
Can you guys help me? Any help is appreciated.
Security problems?
Developers should validate all URLs before passing them to this
function.
For local content running in a browser, calls to the navigateToURL()
method that specify a "javascript:" pseudo-protocol (via a URLRequest
object passed as the first parameter) are only permitted if the SWF
file and the containing web page (if there is one) are in the
local-trusted security sandbox. Some browsers do not support using the
javascript protocol with the navigateToURL() method. Instead, consider
using the call() method of the ExternalInterface API to invoke
JavaScript methods within the enclosing HTML page.
source: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#navigateToURL()
EDIT:
Since javascript is not permitted out of the sandbox, you can try with ExternalInterface:
ExternalInterface.call("javascript_functionname", "mypage.html");
In the parameters for publishing:
'allowScriptAccess', 'always',
You can only test this on your server not locally.
I'd suggest double checking the security settings (right click on flash container->Global Settings-> Advanced -> Trusted Location Settings). Also make sure your html file contains the javascript function you're trying to execute and look for blocked pop-up notifications in the browser. Maybe you just don't allow pop-ups to run.