Copy window.location.href to clipboard from extension












0















I am trying to copy 'window.location.href' e.g. the URL of the current page to clipboard from my extension.



My issue is that when I copy the URL to clipboard, it is the extensions URL that is copied and not the page I am visiting.



Extensionbar:



<!DOCTYPE HTML>
<html>
<head>
<button onclick="copyFunction();">Copy</button>
<script type="text/javascript">
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}
</script>
</head>
</html>


From my understanding the solution should be this, but I fear I am too clueless how to proceed: https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts



This is how I (tried to) proceed, by creating a global.html page and an injected script.



Global page:



<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", copyFunction, false);

function copyFunctionEvent(event) {
if (event.command == "CopyToClipboard") {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("CopyToClipboard", "all");

}
}
</script>


Injected script:



function myextension_openAll(event){
if (event.name == 'CopyToClipboard'){
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}

}
safari.self.addEventListener("message", myextension_openAll, true);


Actual:
safari-extension://com.myextension-0000000000/abc123/extensionbar.html



Expected:
http://www.google.com (e.g. if current tab)










share|improve this question


















  • 1





    Maybe safari.application.activeBrowserWindow.activeTab.url ? I know nothing about Safari extensions and that answer has 0 upvote so just in case it helps.

    – Jeto
    Dec 30 '18 at 8:52











  • Thanks Jeto, I have also tried with this but the issue for me is mainly that I am not sure how to communicate between my extensionbar.html, the global.html and the injected script correctly. I'm afraid I'm lost. :)

    – Joe Berg
    Dec 30 '18 at 13:57
















0















I am trying to copy 'window.location.href' e.g. the URL of the current page to clipboard from my extension.



My issue is that when I copy the URL to clipboard, it is the extensions URL that is copied and not the page I am visiting.



Extensionbar:



<!DOCTYPE HTML>
<html>
<head>
<button onclick="copyFunction();">Copy</button>
<script type="text/javascript">
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}
</script>
</head>
</html>


From my understanding the solution should be this, but I fear I am too clueless how to proceed: https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts



This is how I (tried to) proceed, by creating a global.html page and an injected script.



Global page:



<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", copyFunction, false);

function copyFunctionEvent(event) {
if (event.command == "CopyToClipboard") {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("CopyToClipboard", "all");

}
}
</script>


Injected script:



function myextension_openAll(event){
if (event.name == 'CopyToClipboard'){
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}

}
safari.self.addEventListener("message", myextension_openAll, true);


Actual:
safari-extension://com.myextension-0000000000/abc123/extensionbar.html



Expected:
http://www.google.com (e.g. if current tab)










share|improve this question


















  • 1





    Maybe safari.application.activeBrowserWindow.activeTab.url ? I know nothing about Safari extensions and that answer has 0 upvote so just in case it helps.

    – Jeto
    Dec 30 '18 at 8:52











  • Thanks Jeto, I have also tried with this but the issue for me is mainly that I am not sure how to communicate between my extensionbar.html, the global.html and the injected script correctly. I'm afraid I'm lost. :)

    – Joe Berg
    Dec 30 '18 at 13:57














0












0








0








I am trying to copy 'window.location.href' e.g. the URL of the current page to clipboard from my extension.



My issue is that when I copy the URL to clipboard, it is the extensions URL that is copied and not the page I am visiting.



Extensionbar:



<!DOCTYPE HTML>
<html>
<head>
<button onclick="copyFunction();">Copy</button>
<script type="text/javascript">
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}
</script>
</head>
</html>


From my understanding the solution should be this, but I fear I am too clueless how to proceed: https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts



This is how I (tried to) proceed, by creating a global.html page and an injected script.



Global page:



<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", copyFunction, false);

function copyFunctionEvent(event) {
if (event.command == "CopyToClipboard") {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("CopyToClipboard", "all");

}
}
</script>


Injected script:



function myextension_openAll(event){
if (event.name == 'CopyToClipboard'){
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}

}
safari.self.addEventListener("message", myextension_openAll, true);


Actual:
safari-extension://com.myextension-0000000000/abc123/extensionbar.html



Expected:
http://www.google.com (e.g. if current tab)










share|improve this question














I am trying to copy 'window.location.href' e.g. the URL of the current page to clipboard from my extension.



My issue is that when I copy the URL to clipboard, it is the extensions URL that is copied and not the page I am visiting.



Extensionbar:



<!DOCTYPE HTML>
<html>
<head>
<button onclick="copyFunction();">Copy</button>
<script type="text/javascript">
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}
</script>
</head>
</html>


From my understanding the solution should be this, but I fear I am too clueless how to proceed: https://developer.apple.com/documentation/safariservices/safari_app_extensions/passing_messages_between_safari_app_extensions_and_injected_scripts



This is how I (tried to) proceed, by creating a global.html page and an injected script.



Global page:



<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", copyFunction, false);

function copyFunctionEvent(event) {
if (event.command == "CopyToClipboard") {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("CopyToClipboard", "all");

}
}
</script>


Injected script:



function myextension_openAll(event){
if (event.name == 'CopyToClipboard'){
function copyFunction() {
var inputDump = document.createElement('input'),
hrefText = window.location.href;
document.body.appendChild(inputDump);
inputDump.value = hrefText;
inputDump.select();
document.execCommand('copy');
document.body.removeChild(inputDump);
}

}
safari.self.addEventListener("message", myextension_openAll, true);


Actual:
safari-extension://com.myextension-0000000000/abc123/extensionbar.html



Expected:
http://www.google.com (e.g. if current tab)







javascript html safari-extension






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 8:48









Joe BergJoe Berg

827




827








  • 1





    Maybe safari.application.activeBrowserWindow.activeTab.url ? I know nothing about Safari extensions and that answer has 0 upvote so just in case it helps.

    – Jeto
    Dec 30 '18 at 8:52











  • Thanks Jeto, I have also tried with this but the issue for me is mainly that I am not sure how to communicate between my extensionbar.html, the global.html and the injected script correctly. I'm afraid I'm lost. :)

    – Joe Berg
    Dec 30 '18 at 13:57














  • 1





    Maybe safari.application.activeBrowserWindow.activeTab.url ? I know nothing about Safari extensions and that answer has 0 upvote so just in case it helps.

    – Jeto
    Dec 30 '18 at 8:52











  • Thanks Jeto, I have also tried with this but the issue for me is mainly that I am not sure how to communicate between my extensionbar.html, the global.html and the injected script correctly. I'm afraid I'm lost. :)

    – Joe Berg
    Dec 30 '18 at 13:57








1




1





Maybe safari.application.activeBrowserWindow.activeTab.url ? I know nothing about Safari extensions and that answer has 0 upvote so just in case it helps.

– Jeto
Dec 30 '18 at 8:52





Maybe safari.application.activeBrowserWindow.activeTab.url ? I know nothing about Safari extensions and that answer has 0 upvote so just in case it helps.

– Jeto
Dec 30 '18 at 8:52













Thanks Jeto, I have also tried with this but the issue for me is mainly that I am not sure how to communicate between my extensionbar.html, the global.html and the injected script correctly. I'm afraid I'm lost. :)

– Joe Berg
Dec 30 '18 at 13:57





Thanks Jeto, I have also tried with this but the issue for me is mainly that I am not sure how to communicate between my extensionbar.html, the global.html and the injected script correctly. I'm afraid I'm lost. :)

– Joe Berg
Dec 30 '18 at 13:57












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976304%2fcopy-window-location-href-to-clipboard-from-extension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976304%2fcopy-window-location-href-to-clipboard-from-extension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'