How to get param values from deeplinks - javascript

I open my app with a deeplink
myscheme://?param1=value1&param2=value2
How can I get the value of the parameters? I found different posts that treat this subject but the once I tried works on http links I think, I alwayse get a warning telling me that BlobURL object is not supported yet.
var url = new URL(data);
alert(url.searchParams.get("param1"));

I have tried your example, and it works, with slight adjustment:
let myscheme = 'http://www.example.com/?param1=value1&param2=value2'
var url = new URL(myscheme);
alert(url.searchParams.get("param1"));
More details can be found here.

Related

URL with URL parameters gets concatenated in browsers

I have the following code:
const url = 'https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=' +
encodeURIComponent('https://dev.mysite.com/google_oauth2/') +
'&scope=https://www.googleapis.com/auth/drive.file&client_id=myclientid'
window.open(url, "", "width=700,height=500")
And the super-strange behaviour is that if I clear the browser history and run this code for the first time, it works fine, BUT THEN if I run for the second and subsequent times, the urls gets concatenated and I see
https://dev.mysite.com/google_oauth2/?code=mycode&scope=https://www.googleapis.com/auth/drive.file
instead of
https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https://dev.mysite.com%2Fgoogle_oauth2%&scope=https://www.googleapis.com/auth/drive.file&client_id=myclientid
Does anyone know why this happens ?
BTW, I am using React.JS, but I don't think it has something to do with this...
Resolved. Was a Google-specific issue
I would suggest you use a more appropriate URL and URLSearchParams for such purpose: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
and https://developer.mozilla.org/en-US/docs/Web/API/URL
So your code would be:
const url = new URL('https://accounts.google.com/o/oauth2/v2/auth')
url.searchParams.append('redirect_uri', 'https://dev.mysite.com/google_oauth2/');
url.searchParams.append('scope', 'https://www.googleapis.com/auth/drive.file');
url.searchParams.append('client_id', 'myclientid');
And url.href would be:
https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https%3A%2F%2Fdev.mysite.com%2Fgoogle_oauth2%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file&client_id=myclientid

Passing URL in URL with Electron?

I'm using Electron for a project of mine. I need to pass an URL between windows, which I'm doing by using the URL the following way:
function openWindow(url) {
url=encodeURIComponent(url);
const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;
var win = new BrowserWindow({ width: 800, height: 600 });
win.loadURL('file://' + __dirname + '/otherwindow.html?url=' + url);
}
On the receiving end (in otherwindow.html) I get the parameter this way:
var urlParam = function(name, w){
w = w || window;
var rx = new RegExp('[\?]'+name+'=([^\#]+)'),
val = w.location.search.match(rx);
return !val ? '':val[1];
}
I call this function as:
var decoded=decodeURIComponent(urlParam('url'));
And this all works fine. It's kind of ugly right now, but it works. Or so it seems. If I print out the decoded URL to the console it displays correctly. It seems to be fine, if I open up an Electron window with it, it displays the destination with no problem what so ever.
Here's the catch. I'm using wcjs-player for this project. In the destination page (otherwindow.html) is a wcjs-player instance, which would take the decoded URL and play the media located there with .addPlaylist(); or .vlc.play();.
It works fine if I put the destination URL in a variable in the same page then l put it as a parameter to these 2 functions, even works if I use encodeURIComponent(); and then decodeURIComponent(); on said variable, so encoding the URL is not the problem. I even tried with a base64 encoding to pass between the pages, no success.
So judging from all this, I reckon that the problem is not the encoding itself, but the passing between pages. My (probably wrong) theory is that the URL might get somehow very slightly altered (losing/gaining some information, special characters maybe?), which wcjs-player is not prepared to handle, but Chromium is (since there's no problem with the Electron window using the result URL).
I have no idea to fix this, I've tried all my ideas. Did a fair bit of searching, but didn't really find anything useful. I can solve it another way, but that would involve opening and processing the same file twice - in both windows - which I'm trying to avoid.
I've pretty new to Javascript overall, so please excuse me if I'm missing something trivial. Any help is appreciated!

couch.js allDocs() always returns same result

I'm developing an application for Windows 8 using Javascript. I'm using couchDB as a data provider with the couch.js wrapper.
If I access my existing database "test" like this:
CouchDB.urlPrefix = "http://127.0.0.1:5984";
var db = new CouchDB("test");
var docs = db.allDocs();
var changes = db.changes();
var thing = {};
db.save(thing);
var docs2 = db.allDocs();
var changes2 = db.changes();
Then docs == docs2 and changes == changes2, even though "thing" is saved correctly. This does not seem right to me. What am I doing wrong? I went through the couch.js methods and they seem to be generating the correct request.
IE aggressively caches, and since the request is the same the second time it just gives you the result from the first time around.
This has been fixed in jquery.couch.js, but not in couch.js.
The fix is to append something to the end of the request that makes it unique.
I did "?_=" + Date.now().

Change URL params or remove some part of URL with jquery

I have this URL and wanting to know how can I remove this section from it via a jQuery event.
Need to remove:
&activities_id=13&session_id=14&back=1
Original URL:
http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1
EDIT
Sorry i think i havent included the most important section. I should change the Address BAR url not a normal string.
for example, if i have this url in the address bar - http://somedomain.com/ijob-css/index.php/ after change, address bar should contain http://somedomain.com/xxx=111, without page refreshing.
Do you mean you want the URL without the query parameter part? If then see if this helps.
var test = 'http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1';
alert(test.substring(0, test.indexOf('?')));
If you want until first query parameter name then just seek until index of &
Update :
If you are using HTML5 then what you ask is possible. Check browser history manipulation. You can find details about this here.
I believe replaceState() is the answer for your problem. However it is not supported in all browsers/versions. History.js wraps HTML5 state features and provides additional support for HTML4 browsers.
Try this out
var new_url = old_url.substring(0, old_url.indexOf('&'));
Example: http://jsfiddle.net/SjrqF/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.slice( 0, url.indexOf('&') );
or:
Example: http://jsfiddle.net/SjrqF/1/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.split( '&' )[0];
var lastPart = 'query=';
var url = 'http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1'.split(lastPart)[0] + lastPart;
var index = original_url.indexOf("=");
var new_url = original_url.substring(0,index+1);
See below.
var positionToSubstring = this.location.href.indexOf('&');
var newURI = this.location.href.substring(0, positionToSubstring);
use this
var test='http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1';
test=test.split('&')[0];
console.log(test);
outputs
http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=

Retrieving the favicon url of the websites from firefox extension

I want to retrieve the favicon url of the website once it is loaded. How can I implement this for my firefox extension.
You can use nsIFaviconService, it caches favicons for known pages. Along these lines:
var faviconService = Components.classes["#mozilla.org/browser/favicon-service;1"]
.getService(Components.interfaces.nsIFaviconService);
var favicon = faviconService.getFaviconImageForPage(gBrowser.currentURI);
alert(favicon.spec);
Please note that it works with nsIURI objects, not with strings. You can use nsIIOService.newURI() to get an nsIURI object from a string.
Yes, I realize that I am duplicating karthik's answer - but it has no explanation and only a bogus code example.
https://developer.mozilla.org/en/nsIFaviconService
https://developer.mozilla.org/en/Using_the_Places_favicon_service
Please read the page carefully. You can use the service defined below:
nsIServiceManager serviceManager =
Mozilla.getInstance().getServiceManager();
nsIFaviconService service =
(nsIFaviconService)serviceManager.getServiceByContractID("#mozilla.org/brows
er/favicon-service;1", nsIFaviconService.NS_IFAVICONSERVICE_IID);

Categories