how twitter passes query variables? - url

I notice that when I browse pages in Twitter. Instead of having some like twitter.com/home.php?var1=2&asjfaj...etc. (the most common way), the link is just Twitter.com/#home or twitter.com/inbox or twitter.com/followers. my guess is, they use sessions variables to pass information across pages/ links. is it a good way to do it? what are the pros and cons of using session or the url query to pass data across pages?

They're loaded via AJAX. The #home etc. in the URL allows bookmarking and browser history - if you go to http://twitter.com/#replies you get the replies page correctly, as their JavaScript code looks for document.location.hash's value and loads the right page.
Gmail also does this with the document hash, if you want another example.

ajax. lot of ajax!
EDIT
this is what I use to keep a history
<script>
function openOption(opened) {
if (!opened)
var selectedTab = document.location.hash;
else
var selectedTab = opened;
if (selectedTab != null && selectedTab.length > 0) {
selectedTab = (selectedTab[0] == '#') ? selectedTab.substring(1, selectedTab.length) : selectedTab;
if ($(selectedTab)) {
toggleLayer_on(selectedTab);
$(selectedTab).focus();
$(selectedTab).scrollTo();
}
}
}
openOption();
</script>

Related

Durable Id assignment in URL not working in lightning

Problem Statement: To auto-populate the lookup field I use durable Id assignment with name. For e.g. https://sales--dev.my.salesforce.com/m2p/e?CF00N0l0000051XXX=Contract-00000XXX&inline=1
Notice this -> CF00N0l0000051XXX=Contract-00000XXX ~ durableId=recordName In url.
Now, when the user clicks the New button to create a record on the VF page above URL is loaded in classic and populates the Name in lookup like this
Trying to solve: In lightning, URL is getting overridden by this URL
https://sales--dev.lightning.force.com/lightning/o/objectName/new?count=2 Is there a way to achieve the same URL in lightning?
Do you really need it to be an URL hack? Can'y your thing be a quick action? The url prepopulation would be more reliable there and work everywhere.
URL hacking in lightning is bit simpler, you use field API names instead of IDs. These are decent tutorial: https://www.salesforceben.com/salesforce-url-hacking-for-lightning-tutorial/, https://sfdcdevelopers.com/2020/02/26/url-trick-in-salesforce-lightning/
So, how do you know where you are, in Classic or LEX. Which URL to use? Have a look at UiThemeDisplayed variable, available in Visualforce and in Apex's UserInfo class.
IF($User.UIThemeDisplayed == 'Theme4d' || $User.UIThemeDisplayed == 'Theme4t' || $User.UIThemeDisplayed == 'Theme4u',
'link for lightning',
'link for classic'
)
Working approach:
Created a controller for VF page:
global PageReference newParty() {
PageReference pageRef;
pageRef = new PageReference('/lightning/o/Party/new?defaultFieldValues=Contract='+contractID);
return pageRef
You can absolutely do this with a button / URL hack in lightning with the Spring '20 release. The URL can use "defaultFieldValues="
https://www.salesforceben.com/salesforce-url-hacking-for-lightning-tutorial/

Instagram API request single username

I'm using this way to get the username data from Instagram:
https://api.instagram.com/v1/users/search?q=[USERNAME]&client_id=[CLIENT ID]
It works fine, but has a flaw - the username search actually gets ALL usernames starting with the string you set.
Why/How is that happening ?
There is a limit of 52 username results, so is there a way to increase it, because if you search "asdasd" (which is an existing account!) you would get probably a million accounts ?
Is there a work-around, because I want to search for the exact username ?
So it seems there is only this API for this functionality. You can simply use this workaround: make your request as you are doing it right now, then you can filter out the single item you need. You can iterate through the list of users, and only keep the one, where the username is exactly the same as you have specified.
SOLUTION:
Put the username in quotations like this (username: asdasdasd):
https://api.instagram.com/v1/users/search?q="asdasdasd"&client_id=[CLIENT ID]
Which results in (the results that interests you is highlighted):
- if you don't use the quotations there's a big chance that the desired username won't appear in the results!
If there are more than one results use this code the iterate through the response data to find your one and get it's ID, full_name etc.
function getUserID() {
//send request for the user info on click
$('.btn-user-request').click(function(){
var searchTerm = $('.input-user-request').val();
if(searchTerm == ''){
$('.user-id-value').html('Enter a username!');
}
else{
$.ajax({
url: "https://api.instagram.com/v1/users/search?q=\"" + searchTerm + "\"&client_id=5fc90c90b885487485125d6df440fefd",
dataType: 'jsonp'
}).done(function(data) {
if(data.data[0] == []){
$('.user-id-value').html('No username found!');
}
else{
for(i=0;i<data.data.length;i++){
var userInformation = data.data[i];
if(userInformation.username == searchTerm){
$('.user-id-value').html(userInformation.id);
break;
}
}
}
});
}
});
//simulate the button click, on click of the Enter key
$(".input-user-request").on('keydown', function(){
if(event.keyCode == 13){
$(".btn-user-request").click();
}
});
}
See JSFiddle
(for some reason the code doesn't format properly)
I would suggest to try username "jack".
In fact none of suggested methods work for this.
I tried with max_id, min_id, it does not work either.
So it is exact flaw in API, and there is no such documentation on Instagram API to help to solve this.
So finding exact user id by it's username is quite a problem for popular names.
The only one solution for today I found is parsing user's Instagram HTML page and get it's id from there. This is very stupid, but this is only one solution which works in all cases right now :(
It's simple:
https://www.instagram.com/{username}/?__a=1

Rails search form doesn't work if page has another form

I have a search form in my navbar, so it's on all pages of the application. If i go to a page that has a form like the user's edit page or create a new post page the search doesn't work. How come?? How can i fix this??
EDIT (read comments)
// Make sure search form isn't blank
$("form.navbar-search").submit(function(){
var isFormValid = true;
$("input").each(function(){
if ($.trim($(this).val()).length == 0){
isFormValid = false;
}
else{
$(this).removeClass("highlight");
}
});
return isFormValid;
});
There could be a million reasons, and you haven't given us much to go on. What do you mean by "doesn't work"? Do you get JS errors in your browser console? Application errors in your Rails log?

Modify URL before loading page in firefox

I want to prefix URLs which match my patterns. When I open a new tab in Firefox and enter a matching URL the page should not be loaded normally, the URL should first be modified and then loading the page should start.
Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?
Browsing the HTTPS Everywhere add-on suggests the following steps:
Register an observer for the "http-on-modify-request" observer topic with nsIObserverService
Proceed if the subject of your observer notification is an instance of nsIHttpChannel and subject.URI.spec (the URL) matches your criteria
Create a new nsIStandardURL
Create a new nsIHttpChannel
Replace the old channel with the new. The code for doing this in HTTPS Everywhere is quite dense and probably much more than you need. I'd suggest starting with chrome/content/IOUtils.js.
Note that you should register a single "http-on-modify-request" observer for your entire application, which means you should put it in an XPCOM component (see HTTPS Everywhere for an example).
The following articles do not solve your problem directly, but they do contain a lot of sample code that you might find helpful:
https://developer.mozilla.org/en/Setting_HTTP_request_headers
https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads
Thanks to Iwburk, I have been able to do this.
We can do this my overriding the nsiHttpChannel with a new one, doing this is slightly complicated but luckily the add-on https-everywhere implements this to force a https connection.
https-everywhere's source code is available here
Most of the code needed for this is in the files
IO Util.js
ChannelReplacement.js
We can work with the above files alone provided we have the basic variables like Cc,Ci set up and the function xpcom_generateQI defined.
var httpRequestObserver =
{
observe: function(subject, topic, data) {
if (topic == "http-on-modify-request") {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
var requestURL = subject.URI.spec;
if(isToBeReplaced(requestURL)) {
var newURL = getURL(requestURL);
ChannelReplacement.runWhenPending(subject, function() {
var cr = new ChannelReplacement(subject, ch);
cr.replace(true,null);
cr.open();
});
}
}
},
get observerService() {
return Components.classes["#mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
},
register: function() {
this.observerService.addObserver(this, "http-on-modify-request", false);
},
unregister: function() {
this.observerService.removeObserver(this, "http-on-modify-request");
}
};
httpRequestObserver.register();
The code will replace the request not redirect.
While I have tested the above code well enough, I am not sure about its implementation. As far I can make out, it copies all the attributes of the requested channel and sets them to the channel to be overridden. After which somehow the output requested by original request is supplied using the new channel.
P.S. I had seen a SO post in which this approach was suggested.
You could listen for the page load event or maybe the DOMContentLoaded event instead. Or you can make an nsIURIContentListener but that's probably more complicated.
Is it possible to modify an URL through a Mozilla Firefox Addon before the page starts loading?
YES it is possible.
Use page-mod of the Addon-SDK by setting contentScriptWhen: "start"
Then after completely preventing the document from getting parsed you can either
fetch a different document from the same domain and inject it in the page.
after some document.URL processing do a location.replace() call
Here is an example of doing 1. https://stackoverflow.com/a/36097573/6085033

Rails 3: How to tell if a user has visited a page before?

I have a Rails app (using Authlogic for authentication) with a simple jquery animation that I only want to run once upon first page load. I know that the key is to check cookies or something to see if the user has visited the page before. Please forgive my n00bness, I know little to nothing about HTTP cookies or sessions.
So, what's the best way to see if a visiting user (even if they haven't logged in) is viewing a page for the first time?
EDIT: Ok, so I realize I wasn't being entirely clear.
I've spent hours looking at similar questions and reading the Rails API Docs for cookies and sessions and I still can't visualize how to implement a visited? function for each page in my site that will only be set to "true" after the user has visited the page the first time. I looked at the supposed "duplicate" question Rails Detect If User's Very First Visit and the respective answers and still can't figure it out.
Here's my "Pages" controller:
def home
#title = "Home"
end
def contact
#title = "Contact Us"
end
dd
And my jquery javascript that does a simple animation:
$(document).ready(function()
{
if (!$.cookie('visited')) {
$('.title .flying-text').css({opacity:0});
$('.title .active-text').animate({
opacity:1,
marginTop: "-150px",
}, 5000);
}
});
I only want it to show if the user HAS NOT visited the page before. I have no idea how to properly set a cookie in Rails, nor where to put it, nor how to make sure that the animation script can access that exact same cookie value. Can someone give me a hand?
~Dan
You can set a cookie like this:
cookies[:welcomed] = {:value => true, :expires => Time.now + 6.months}
and from jquery make a wee function
function readCookie(name) {
var nameEQ = name + "=", ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i += 1) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
and call it #in js
if (readCookie('welcomed') == null){}
Ignore the cookie from Javascript.
Using the question referenced as a duplicate, use a permanent cooke and on the first run, set a class on your body from Rails (let's say first-run). Then, have your javascript check and see if body has class first-run. At that point, you can have javascript execute it's first run code.

Resources