Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
open-source
SemWeb
libview
Commits
de4a659c7c71
Commit
28fcbcae
authored
Jul 06, 2018
by
Laurent Wouters
Browse files
Transform into a web extension
parent
15a9d1e526f4
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/content/main.js
View file @
de4a659c
...
...
@@ -12,10 +12,48 @@ function injectApplication() {
var
appContent
=
document
.
createElement
(
"
div
"
);
appContent
.
id
=
ID
;
appContent
.
style
.
zIndex
=
zIndex
+
10
;
appContent
.
appendChild
(
document
.
createTextNode
(
"
Here!
"
));
appContent
.
appendChild
(
document
.
createTextNode
(
"
Loading ...
"
));
document
.
body
.
insertBefore
(
appContent
,
document
.
body
.
firstChild
);
}
function
doFetchContent
(
contentType
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
resolve
({
contentType
:
contentType
,
content
:
document
.
body
.
innerText
});
});
}
function
doFetchLink
(
link
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
xmlHttp
=
new
XMLHttpRequest
();
xmlHttp
.
onreadystatechange
=
function
()
{
if
(
xmlHttp
.
readyState
==
4
)
{
if
(
xmlHttp
.
status
!=
200
)
{
reject
(
"
HTTP error:
"
+
xmlHttp
.
status
);
}
var
ct
=
xmlHttp
.
getResponseHeader
(
"
Content-Type
"
);
resolve
({
contentType
:
ct
,
content
:
xmlHttp
.
responseText
});
}
}
xmlHttp
.
open
(
"
GET
"
,
link
.
url
,
true
);
xmlHttp
.
setRequestHeader
(
"
Accept
"
,
link
.
type
);
xmlHttp
.
send
();
});
}
function
injectContent
(
data
)
{
var
root
=
document
.
getElementById
(
ID
);
while
(
root
.
hasChildNodes
())
{
root
.
removeChild
(
root
.
lastChild
);
}
root
.
appendChild
(
document
.
createTextNode
(
data
.
content
));
}
function
onUpdateDeactivate
()
{
var
root
=
document
.
getElementById
(
ID
);
if
(
root
==
null
)
...
...
@@ -23,16 +61,28 @@ function onUpdateDeactivate() {
root
.
parentNode
.
removeChild
(
root
);
}
function
onUpdateActivate
(
d
ata
)
{
function
onUpdateActivate
(
linkedD
ata
)
{
onUpdateDeactivate
();
injectApplication
();
var
fetch
=
null
;
if
(
linkedData
.
target
==
"
content
"
)
{
fetch
=
doFetchContent
(
linkedData
.
syntax
);
}
else
{
fetch
=
doFetchLink
(
linkedData
.
target
)
}
fetch
.
then
(
function
(
result
)
{
injectContent
(
result
);
}).
catch
(
function
(
reason
)
{
var
root
=
document
.
getElementById
(
ID
);
root
.
innerText
=
reason
;
});
}
chrome
.
runtime
.
onMessage
.
addListener
(
function
(
request
,
sender
,
sendResponse
)
{
var
activate
=
request
.
isActive
&&
request
.
syntax
!=
""
;
var
activate
=
request
.
isActive
&&
request
.
linkedData
.
syntax
!=
""
;
if
(
activate
)
onUpdateActivate
(
request
);
onUpdateActivate
(
request
.
linkedData
);
else
onUpdateDeactivate
();
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment